unpack_test.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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. "encoding/hex"
  20. "fmt"
  21. "math/big"
  22. "reflect"
  23. "strconv"
  24. "strings"
  25. "testing"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/stretchr/testify/require"
  28. )
  29. type unpackTest struct {
  30. def string // ABI definition JSON
  31. enc string // evm return data
  32. want interface{} // the expected output
  33. err string // empty or error if expected
  34. }
  35. func (test unpackTest) checkError(err error) error {
  36. if err != nil {
  37. if len(test.err) == 0 {
  38. return fmt.Errorf("expected no err but got: %v", err)
  39. } else if err.Error() != test.err {
  40. return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
  41. }
  42. } else if len(test.err) > 0 {
  43. return fmt.Errorf("expected err: %v but got none", test.err)
  44. }
  45. return nil
  46. }
  47. var unpackTests = []unpackTest{
  48. {
  49. def: `[{ "type": "bool" }]`,
  50. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  51. want: true,
  52. },
  53. {
  54. def: `[{ "type": "bool" }]`,
  55. enc: "0000000000000000000000000000000000000000000000000000000000000000",
  56. want: false,
  57. },
  58. {
  59. def: `[{ "type": "bool" }]`,
  60. enc: "0000000000000000000000000000000000000000000000000001000000000001",
  61. want: false,
  62. err: "abi: improperly encoded boolean value",
  63. },
  64. {
  65. def: `[{ "type": "bool" }]`,
  66. enc: "0000000000000000000000000000000000000000000000000000000000000003",
  67. want: false,
  68. err: "abi: improperly encoded boolean value",
  69. },
  70. {
  71. def: `[{"type": "uint32"}]`,
  72. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  73. want: uint32(1),
  74. },
  75. {
  76. def: `[{"type": "uint32"}]`,
  77. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  78. want: uint16(0),
  79. err: "abi: cannot unmarshal uint32 in to uint16",
  80. },
  81. {
  82. def: `[{"type": "uint17"}]`,
  83. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  84. want: uint16(0),
  85. err: "abi: cannot unmarshal *big.Int in to uint16",
  86. },
  87. {
  88. def: `[{"type": "uint17"}]`,
  89. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  90. want: big.NewInt(1),
  91. },
  92. {
  93. def: `[{"type": "int32"}]`,
  94. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  95. want: int32(1),
  96. },
  97. {
  98. def: `[{"type": "int32"}]`,
  99. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  100. want: int16(0),
  101. err: "abi: cannot unmarshal int32 in to int16",
  102. },
  103. {
  104. def: `[{"type": "int17"}]`,
  105. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  106. want: int16(0),
  107. err: "abi: cannot unmarshal *big.Int in to int16",
  108. },
  109. {
  110. def: `[{"type": "int17"}]`,
  111. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  112. want: big.NewInt(1),
  113. },
  114. {
  115. def: `[{"type": "int256"}]`,
  116. enc: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  117. want: big.NewInt(-1),
  118. },
  119. {
  120. def: `[{"type": "address"}]`,
  121. enc: "0000000000000000000000000100000000000000000000000000000000000000",
  122. want: common.Address{1},
  123. },
  124. {
  125. def: `[{"type": "bytes32"}]`,
  126. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  127. want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  128. },
  129. {
  130. def: `[{"type": "bytes"}]`,
  131. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  132. want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  133. },
  134. {
  135. def: `[{"type": "bytes"}]`,
  136. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  137. want: [32]byte{},
  138. err: "abi: cannot unmarshal []uint8 in to [32]uint8",
  139. },
  140. {
  141. def: `[{"type": "bytes32"}]`,
  142. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  143. want: []byte(nil),
  144. err: "abi: cannot unmarshal [32]uint8 in to []uint8",
  145. },
  146. {
  147. def: `[{"type": "bytes32"}]`,
  148. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  149. want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  150. },
  151. {
  152. def: `[{"type": "function"}]`,
  153. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  154. want: [24]byte{1},
  155. },
  156. // slices
  157. {
  158. def: `[{"type": "uint8[]"}]`,
  159. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  160. want: []uint8{1, 2},
  161. },
  162. {
  163. def: `[{"type": "uint8[2]"}]`,
  164. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  165. want: [2]uint8{1, 2},
  166. },
  167. // multi dimensional, if these pass, all types that don't require length prefix should pass
  168. {
  169. def: `[{"type": "uint8[][]"}]`,
  170. enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000E0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  171. want: [][]uint8{{1, 2}, {1, 2}},
  172. },
  173. {
  174. def: `[{"type": "uint8[2][2]"}]`,
  175. enc: "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  176. want: [2][2]uint8{{1, 2}, {1, 2}},
  177. },
  178. {
  179. def: `[{"type": "uint8[][2]"}]`,
  180. enc: "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
  181. want: [2][]uint8{{1}, {1}},
  182. },
  183. {
  184. def: `[{"type": "uint8[2][]"}]`,
  185. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  186. want: [][2]uint8{{1, 2}},
  187. },
  188. {
  189. def: `[{"type": "uint8[2][]"}]`,
  190. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  191. want: [][2]uint8{{1, 2}, {1, 2}},
  192. },
  193. {
  194. def: `[{"type": "uint16[]"}]`,
  195. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  196. want: []uint16{1, 2},
  197. },
  198. {
  199. def: `[{"type": "uint16[2]"}]`,
  200. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  201. want: [2]uint16{1, 2},
  202. },
  203. {
  204. def: `[{"type": "uint32[]"}]`,
  205. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  206. want: []uint32{1, 2},
  207. },
  208. {
  209. def: `[{"type": "uint32[2]"}]`,
  210. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  211. want: [2]uint32{1, 2},
  212. },
  213. {
  214. def: `[{"type": "uint32[2][3][4]"}]`,
  215. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018",
  216. want: [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}}},
  217. },
  218. {
  219. def: `[{"type": "uint64[]"}]`,
  220. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  221. want: []uint64{1, 2},
  222. },
  223. {
  224. def: `[{"type": "uint64[2]"}]`,
  225. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  226. want: [2]uint64{1, 2},
  227. },
  228. {
  229. def: `[{"type": "uint256[]"}]`,
  230. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  231. want: []*big.Int{big.NewInt(1), big.NewInt(2)},
  232. },
  233. {
  234. def: `[{"type": "uint256[3]"}]`,
  235. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  236. want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  237. },
  238. {
  239. def: `[{"type": "int8[]"}]`,
  240. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  241. want: []int8{1, 2},
  242. },
  243. {
  244. def: `[{"type": "int8[2]"}]`,
  245. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  246. want: [2]int8{1, 2},
  247. },
  248. {
  249. def: `[{"type": "int16[]"}]`,
  250. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  251. want: []int16{1, 2},
  252. },
  253. {
  254. def: `[{"type": "int16[2]"}]`,
  255. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  256. want: [2]int16{1, 2},
  257. },
  258. {
  259. def: `[{"type": "int32[]"}]`,
  260. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  261. want: []int32{1, 2},
  262. },
  263. {
  264. def: `[{"type": "int32[2]"}]`,
  265. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  266. want: [2]int32{1, 2},
  267. },
  268. {
  269. def: `[{"type": "int64[]"}]`,
  270. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  271. want: []int64{1, 2},
  272. },
  273. {
  274. def: `[{"type": "int64[2]"}]`,
  275. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  276. want: [2]int64{1, 2},
  277. },
  278. {
  279. def: `[{"type": "int256[]"}]`,
  280. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  281. want: []*big.Int{big.NewInt(1), big.NewInt(2)},
  282. },
  283. {
  284. def: `[{"type": "int256[3]"}]`,
  285. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  286. want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  287. },
  288. // struct outputs
  289. {
  290. def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`,
  291. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  292. want: struct {
  293. Int1 *big.Int
  294. Int2 *big.Int
  295. }{big.NewInt(1), big.NewInt(2)},
  296. },
  297. {
  298. def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
  299. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  300. want: struct {
  301. Int1 *big.Int
  302. Int2 *big.Int
  303. }{},
  304. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  305. },
  306. {
  307. def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
  308. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  309. want: struct {
  310. Int1 *big.Int
  311. Int2 *big.Int
  312. }{},
  313. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  314. },
  315. {
  316. def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
  317. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  318. want: struct {
  319. Int1 *big.Int
  320. Int2 *big.Int
  321. }{},
  322. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  323. },
  324. {
  325. def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
  326. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  327. want: struct {
  328. Int1 *big.Int
  329. Int2 *big.Int
  330. }{},
  331. err: "abi: purely underscored output cannot unpack to struct",
  332. },
  333. }
  334. func TestUnpack(t *testing.T) {
  335. for i, test := range unpackTests {
  336. t.Run(strconv.Itoa(i), func(t *testing.T) {
  337. def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  338. abi, err := JSON(strings.NewReader(def))
  339. if err != nil {
  340. t.Fatalf("invalid ABI definition %s: %v", def, err)
  341. }
  342. encb, err := hex.DecodeString(test.enc)
  343. if err != nil {
  344. t.Fatalf("invalid hex: %s" + test.enc)
  345. }
  346. outptr := reflect.New(reflect.TypeOf(test.want))
  347. err = abi.Unpack(outptr.Interface(), "method", encb)
  348. if err := test.checkError(err); err != nil {
  349. t.Errorf("test %d (%v) failed: %v", i, test.def, err)
  350. return
  351. }
  352. out := outptr.Elem().Interface()
  353. if !reflect.DeepEqual(test.want, out) {
  354. t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
  355. }
  356. })
  357. }
  358. }
  359. func TestUnpackSetDynamicArrayOutput(t *testing.T) {
  360. abi, err := JSON(strings.NewReader(`[{"constant":true,"inputs":[],"name":"testDynamicFixedBytes15","outputs":[{"name":"","type":"bytes15[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"testDynamicFixedBytes32","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"}]`))
  361. if err != nil {
  362. t.Fatal(err)
  363. }
  364. var (
  365. marshalledReturn32 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783132333435363738393000000000000000000000000000000000000000003078303938373635343332310000000000000000000000000000000000000000")
  366. marshalledReturn15 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783031323334350000000000000000000000000000000000000000000000003078393837363534000000000000000000000000000000000000000000000000")
  367. out32 [][32]byte
  368. out15 [][15]byte
  369. )
  370. // test 32
  371. err = abi.Unpack(&out32, "testDynamicFixedBytes32", marshalledReturn32)
  372. if err != nil {
  373. t.Fatal(err)
  374. }
  375. if len(out32) != 2 {
  376. t.Fatalf("expected array with 2 values, got %d", len(out32))
  377. }
  378. expected := common.Hex2Bytes("3078313233343536373839300000000000000000000000000000000000000000")
  379. if !bytes.Equal(out32[0][:], expected) {
  380. t.Errorf("expected %x, got %x\n", expected, out32[0])
  381. }
  382. expected = common.Hex2Bytes("3078303938373635343332310000000000000000000000000000000000000000")
  383. if !bytes.Equal(out32[1][:], expected) {
  384. t.Errorf("expected %x, got %x\n", expected, out32[1])
  385. }
  386. // test 15
  387. err = abi.Unpack(&out15, "testDynamicFixedBytes32", marshalledReturn15)
  388. if err != nil {
  389. t.Fatal(err)
  390. }
  391. if len(out15) != 2 {
  392. t.Fatalf("expected array with 2 values, got %d", len(out15))
  393. }
  394. expected = common.Hex2Bytes("307830313233343500000000000000")
  395. if !bytes.Equal(out15[0][:], expected) {
  396. t.Errorf("expected %x, got %x\n", expected, out15[0])
  397. }
  398. expected = common.Hex2Bytes("307839383736353400000000000000")
  399. if !bytes.Equal(out15[1][:], expected) {
  400. t.Errorf("expected %x, got %x\n", expected, out15[1])
  401. }
  402. }
  403. type methodMultiOutput struct {
  404. Int *big.Int
  405. String string
  406. }
  407. func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
  408. const definition = `[
  409. { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  410. var expected = methodMultiOutput{big.NewInt(1), "hello"}
  411. abi, err := JSON(strings.NewReader(definition))
  412. require.NoError(err)
  413. // using buff to make the code readable
  414. buff := new(bytes.Buffer)
  415. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  416. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  417. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  418. buff.Write(common.RightPadBytes([]byte(expected.String), 32))
  419. return abi, buff.Bytes(), expected
  420. }
  421. func TestMethodMultiReturn(t *testing.T) {
  422. type reversed struct {
  423. String string
  424. Int *big.Int
  425. }
  426. abi, data, expected := methodMultiReturn(require.New(t))
  427. bigint := new(big.Int)
  428. var testCases = []struct {
  429. dest interface{}
  430. expected interface{}
  431. error string
  432. name string
  433. }{{
  434. &methodMultiOutput{},
  435. &expected,
  436. "",
  437. "Can unpack into structure",
  438. }, {
  439. &reversed{},
  440. &reversed{expected.String, expected.Int},
  441. "",
  442. "Can unpack into reversed structure",
  443. }, {
  444. &[]interface{}{&bigint, new(string)},
  445. &[]interface{}{&expected.Int, &expected.String},
  446. "",
  447. "Can unpack into a slice",
  448. }, {
  449. &[2]interface{}{&bigint, new(string)},
  450. &[2]interface{}{&expected.Int, &expected.String},
  451. "",
  452. "Can unpack into an array",
  453. }, {
  454. &[]interface{}{new(int), new(int)},
  455. &[]interface{}{&expected.Int, &expected.String},
  456. "abi: cannot unmarshal *big.Int in to int",
  457. "Can not unpack into a slice with wrong types",
  458. }, {
  459. &[]interface{}{new(int)},
  460. &[]interface{}{},
  461. "abi: insufficient number of elements in the list/array for unpack, want 2, got 1",
  462. "Can not unpack into a slice with wrong types",
  463. }}
  464. for _, tc := range testCases {
  465. tc := tc
  466. t.Run(tc.name, func(t *testing.T) {
  467. require := require.New(t)
  468. err := abi.Unpack(tc.dest, "multi", data)
  469. if tc.error == "" {
  470. require.Nil(err, "Should be able to unpack method outputs.")
  471. require.Equal(tc.expected, tc.dest)
  472. } else {
  473. require.EqualError(err, tc.error)
  474. }
  475. })
  476. }
  477. }
  478. func TestMultiReturnWithArray(t *testing.T) {
  479. const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
  480. abi, err := JSON(strings.NewReader(definition))
  481. if err != nil {
  482. t.Fatal(err)
  483. }
  484. buff := new(bytes.Buffer)
  485. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
  486. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
  487. ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
  488. ret2, ret2Exp := new(uint64), uint64(8)
  489. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  490. t.Fatal(err)
  491. }
  492. if !reflect.DeepEqual(*ret1, ret1Exp) {
  493. t.Error("array result", *ret1, "!= Expected", ret1Exp)
  494. }
  495. if *ret2 != ret2Exp {
  496. t.Error("int result", *ret2, "!= Expected", ret2Exp)
  497. }
  498. }
  499. func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
  500. // Similar to TestMultiReturnWithArray, but with a special case in mind:
  501. // values of nested static arrays count towards the size as well, and any element following
  502. // after such nested array argument should be read with the correct offset,
  503. // so that it does not read content from the previous array argument.
  504. const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
  505. abi, err := JSON(strings.NewReader(definition))
  506. if err != nil {
  507. t.Fatal(err)
  508. }
  509. buff := new(bytes.Buffer)
  510. // construct the test array, each 3 char element is joined with 61 '0' chars,
  511. // to from the ((3 + 61) * 0.5) = 32 byte elements in the array.
  512. buff.Write(common.Hex2Bytes(strings.Join([]string{
  513. "", //empty, to apply the 61-char separator to the first element as well.
  514. "111", "112", "113", "121", "122", "123",
  515. "211", "212", "213", "221", "222", "223",
  516. "311", "312", "313", "321", "322", "323",
  517. "411", "412", "413", "421", "422", "423",
  518. }, "0000000000000000000000000000000000000000000000000000000000000")))
  519. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
  520. ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
  521. {{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
  522. {{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
  523. {{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
  524. {{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
  525. }
  526. ret2, ret2Exp := new(uint64), uint64(0x9876)
  527. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  528. t.Fatal(err)
  529. }
  530. if !reflect.DeepEqual(*ret1, ret1Exp) {
  531. t.Error("array result", *ret1, "!= Expected", ret1Exp)
  532. }
  533. if *ret2 != ret2Exp {
  534. t.Error("int result", *ret2, "!= Expected", ret2Exp)
  535. }
  536. }
  537. func TestUnmarshal(t *testing.T) {
  538. const definition = `[
  539. { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
  540. { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
  541. { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
  542. { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  543. { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  544. { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
  545. { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
  546. { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
  547. { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  548. abi, err := JSON(strings.NewReader(definition))
  549. if err != nil {
  550. t.Fatal(err)
  551. }
  552. buff := new(bytes.Buffer)
  553. // marshall mixed bytes (mixedBytes)
  554. p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
  555. p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
  556. mixedBytes := []interface{}{&p0, &p1}
  557. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  558. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
  559. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
  560. buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
  561. err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
  562. if err != nil {
  563. t.Error(err)
  564. } else {
  565. if !bytes.Equal(p0, p0Exp) {
  566. t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
  567. }
  568. if !bytes.Equal(p1[:], p1Exp) {
  569. t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
  570. }
  571. }
  572. // marshal int
  573. var Int *big.Int
  574. err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  575. if err != nil {
  576. t.Error(err)
  577. }
  578. if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
  579. t.Error("expected Int to be 1 got", Int)
  580. }
  581. // marshal bool
  582. var Bool bool
  583. err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  584. if err != nil {
  585. t.Error(err)
  586. }
  587. if !Bool {
  588. t.Error("expected Bool to be true")
  589. }
  590. // marshal dynamic bytes max length 32
  591. buff.Reset()
  592. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  593. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  594. bytesOut := common.RightPadBytes([]byte("hello"), 32)
  595. buff.Write(bytesOut)
  596. var Bytes []byte
  597. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  598. if err != nil {
  599. t.Error(err)
  600. }
  601. if !bytes.Equal(Bytes, bytesOut) {
  602. t.Errorf("expected %x got %x", bytesOut, Bytes)
  603. }
  604. // marshall dynamic bytes max length 64
  605. buff.Reset()
  606. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  607. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  608. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  609. buff.Write(bytesOut)
  610. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  611. if err != nil {
  612. t.Error(err)
  613. }
  614. if !bytes.Equal(Bytes, bytesOut) {
  615. t.Errorf("expected %x got %x", bytesOut, Bytes)
  616. }
  617. // marshall dynamic bytes max length 64
  618. buff.Reset()
  619. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  620. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  621. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  622. buff.Write(bytesOut)
  623. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  624. if err != nil {
  625. t.Error(err)
  626. }
  627. if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
  628. t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
  629. }
  630. // marshal dynamic bytes output empty
  631. err = abi.Unpack(&Bytes, "bytes", nil)
  632. if err == nil {
  633. t.Error("expected error")
  634. }
  635. // marshal dynamic bytes length 5
  636. buff.Reset()
  637. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  638. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  639. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  640. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  641. if err != nil {
  642. t.Error(err)
  643. }
  644. if !bytes.Equal(Bytes, []byte("hello")) {
  645. t.Errorf("expected %x got %x", bytesOut, Bytes)
  646. }
  647. // marshal dynamic bytes length 5
  648. buff.Reset()
  649. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  650. var hash common.Hash
  651. err = abi.Unpack(&hash, "fixed", buff.Bytes())
  652. if err != nil {
  653. t.Error(err)
  654. }
  655. helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  656. if hash != helloHash {
  657. t.Errorf("Expected %x to equal %x", hash, helloHash)
  658. }
  659. // marshal error
  660. buff.Reset()
  661. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  662. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  663. if err == nil {
  664. t.Error("expected error")
  665. }
  666. err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
  667. if err == nil {
  668. t.Error("expected error")
  669. }
  670. buff.Reset()
  671. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  672. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
  673. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
  674. // marshal int array
  675. var intArray [3]*big.Int
  676. err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
  677. if err != nil {
  678. t.Error(err)
  679. }
  680. var testAgainstIntArray [3]*big.Int
  681. testAgainstIntArray[0] = big.NewInt(1)
  682. testAgainstIntArray[1] = big.NewInt(2)
  683. testAgainstIntArray[2] = big.NewInt(3)
  684. for i, Int := range intArray {
  685. if Int.Cmp(testAgainstIntArray[i]) != 0 {
  686. t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
  687. }
  688. }
  689. // marshal address slice
  690. buff.Reset()
  691. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
  692. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  693. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  694. var outAddr []common.Address
  695. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  696. if err != nil {
  697. t.Fatal("didn't expect error:", err)
  698. }
  699. if len(outAddr) != 1 {
  700. t.Fatal("expected 1 item, got", len(outAddr))
  701. }
  702. if outAddr[0] != (common.Address{1}) {
  703. t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
  704. }
  705. // marshal multiple address slice
  706. buff.Reset()
  707. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
  708. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
  709. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  710. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  711. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
  712. buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
  713. buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
  714. var outAddrStruct struct {
  715. A []common.Address
  716. B []common.Address
  717. }
  718. err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
  719. if err != nil {
  720. t.Fatal("didn't expect error:", err)
  721. }
  722. if len(outAddrStruct.A) != 1 {
  723. t.Fatal("expected 1 item, got", len(outAddrStruct.A))
  724. }
  725. if outAddrStruct.A[0] != (common.Address{1}) {
  726. t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
  727. }
  728. if len(outAddrStruct.B) != 2 {
  729. t.Fatal("expected 1 item, got", len(outAddrStruct.B))
  730. }
  731. if outAddrStruct.B[0] != (common.Address{2}) {
  732. t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
  733. }
  734. if outAddrStruct.B[1] != (common.Address{3}) {
  735. t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
  736. }
  737. // marshal invalid address slice
  738. buff.Reset()
  739. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
  740. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  741. if err == nil {
  742. t.Fatal("expected error:", err)
  743. }
  744. }
  745. func TestOOMMaliciousInput(t *testing.T) {
  746. oomTests := []unpackTest{
  747. {
  748. def: `[{"type": "uint8[]"}]`,
  749. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  750. "0000000000000000000000000000000000000000000000000000000000000003" + // num elems
  751. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  752. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  753. },
  754. { // Length larger than 64 bits
  755. def: `[{"type": "uint8[]"}]`,
  756. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  757. "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
  758. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  759. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  760. },
  761. { // Offset very large (over 64 bits)
  762. def: `[{"type": "uint8[]"}]`,
  763. enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
  764. "0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  765. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  766. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  767. },
  768. { // Offset very large (below 64 bits)
  769. def: `[{"type": "uint8[]"}]`,
  770. enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
  771. "0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  772. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  773. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  774. },
  775. { // Offset negative (as 64 bit)
  776. def: `[{"type": "uint8[]"}]`,
  777. enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
  778. "0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  779. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  780. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  781. },
  782. { // Negative length
  783. def: `[{"type": "uint8[]"}]`,
  784. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  785. "000000000000000000000000000000000000000000000000f000000000000002" + // num elems
  786. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  787. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  788. },
  789. { // Very large length
  790. def: `[{"type": "uint8[]"}]`,
  791. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  792. "0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
  793. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  794. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  795. },
  796. }
  797. for i, test := range oomTests {
  798. def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  799. abi, err := JSON(strings.NewReader(def))
  800. if err != nil {
  801. t.Fatalf("invalid ABI definition %s: %v", def, err)
  802. }
  803. encb, err := hex.DecodeString(test.enc)
  804. if err != nil {
  805. t.Fatalf("invalid hex: %s" + test.enc)
  806. }
  807. _, err = abi.Methods["method"].Outputs.UnpackValues(encb)
  808. if err == nil {
  809. t.Fatalf("Expected error on malicious input, test %d", i)
  810. }
  811. }
  812. }