unpack_test.go 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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": "string[4]"}]`,
  240. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b476f2d657468657265756d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000",
  241. want: [4]string{"Hello", "World", "Go-ethereum", "Ethereum"},
  242. },
  243. {
  244. def: `[{"type": "string[]"}]`,
  245. enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b676f2d657468657265756d000000000000000000000000000000000000000000",
  246. want: []string{"Ethereum", "go-ethereum"},
  247. },
  248. {
  249. def: `[{"type": "int8[]"}]`,
  250. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  251. want: []int8{1, 2},
  252. },
  253. {
  254. def: `[{"type": "int8[2]"}]`,
  255. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  256. want: [2]int8{1, 2},
  257. },
  258. {
  259. def: `[{"type": "int16[]"}]`,
  260. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  261. want: []int16{1, 2},
  262. },
  263. {
  264. def: `[{"type": "int16[2]"}]`,
  265. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  266. want: [2]int16{1, 2},
  267. },
  268. {
  269. def: `[{"type": "int32[]"}]`,
  270. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  271. want: []int32{1, 2},
  272. },
  273. {
  274. def: `[{"type": "int32[2]"}]`,
  275. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  276. want: [2]int32{1, 2},
  277. },
  278. {
  279. def: `[{"type": "int64[]"}]`,
  280. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  281. want: []int64{1, 2},
  282. },
  283. {
  284. def: `[{"type": "int64[2]"}]`,
  285. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  286. want: [2]int64{1, 2},
  287. },
  288. {
  289. def: `[{"type": "int256[]"}]`,
  290. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  291. want: []*big.Int{big.NewInt(1), big.NewInt(2)},
  292. },
  293. {
  294. def: `[{"type": "int256[3]"}]`,
  295. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  296. want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  297. },
  298. // struct outputs
  299. {
  300. def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`,
  301. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  302. want: struct {
  303. Int1 *big.Int
  304. Int2 *big.Int
  305. }{big.NewInt(1), big.NewInt(2)},
  306. },
  307. {
  308. def: `[{"name":"int_one","type":"int256"}]`,
  309. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  310. want: struct {
  311. IntOne *big.Int
  312. }{big.NewInt(1)},
  313. },
  314. {
  315. def: `[{"name":"int__one","type":"int256"}]`,
  316. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  317. want: struct {
  318. IntOne *big.Int
  319. }{big.NewInt(1)},
  320. },
  321. {
  322. def: `[{"name":"int_one_","type":"int256"}]`,
  323. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  324. want: struct {
  325. IntOne *big.Int
  326. }{big.NewInt(1)},
  327. },
  328. {
  329. def: `[{"name":"int_one","type":"int256"}, {"name":"intone","type":"int256"}]`,
  330. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  331. want: struct {
  332. IntOne *big.Int
  333. Intone *big.Int
  334. }{big.NewInt(1), big.NewInt(2)},
  335. },
  336. {
  337. def: `[{"name":"___","type":"int256"}]`,
  338. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  339. want: struct {
  340. IntOne *big.Int
  341. Intone *big.Int
  342. }{},
  343. err: "abi: purely underscored output cannot unpack to struct",
  344. },
  345. {
  346. def: `[{"name":"int_one","type":"int256"},{"name":"IntOne","type":"int256"}]`,
  347. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  348. want: struct {
  349. Int1 *big.Int
  350. Int2 *big.Int
  351. }{},
  352. err: "abi: multiple outputs mapping to the same struct field 'IntOne'",
  353. },
  354. {
  355. def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
  356. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  357. want: struct {
  358. Int1 *big.Int
  359. Int2 *big.Int
  360. }{},
  361. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  362. },
  363. {
  364. def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
  365. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  366. want: struct {
  367. Int1 *big.Int
  368. Int2 *big.Int
  369. }{},
  370. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  371. },
  372. {
  373. def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
  374. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  375. want: struct {
  376. Int1 *big.Int
  377. Int2 *big.Int
  378. }{},
  379. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  380. },
  381. {
  382. def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
  383. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  384. want: struct {
  385. Int1 *big.Int
  386. Int2 *big.Int
  387. }{},
  388. err: "abi: purely underscored output cannot unpack to struct",
  389. },
  390. }
  391. func TestUnpack(t *testing.T) {
  392. for i, test := range unpackTests {
  393. t.Run(strconv.Itoa(i), func(t *testing.T) {
  394. def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  395. abi, err := JSON(strings.NewReader(def))
  396. if err != nil {
  397. t.Fatalf("invalid ABI definition %s: %v", def, err)
  398. }
  399. encb, err := hex.DecodeString(test.enc)
  400. if err != nil {
  401. t.Fatalf("invalid hex: %s" + test.enc)
  402. }
  403. outptr := reflect.New(reflect.TypeOf(test.want))
  404. err = abi.Unpack(outptr.Interface(), "method", encb)
  405. if err := test.checkError(err); err != nil {
  406. t.Errorf("test %d (%v) failed: %v", i, test.def, err)
  407. return
  408. }
  409. out := outptr.Elem().Interface()
  410. if !reflect.DeepEqual(test.want, out) {
  411. t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
  412. }
  413. })
  414. }
  415. }
  416. func TestUnpackSetDynamicArrayOutput(t *testing.T) {
  417. 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"}]`))
  418. if err != nil {
  419. t.Fatal(err)
  420. }
  421. var (
  422. marshalledReturn32 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783132333435363738393000000000000000000000000000000000000000003078303938373635343332310000000000000000000000000000000000000000")
  423. marshalledReturn15 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783031323334350000000000000000000000000000000000000000000000003078393837363534000000000000000000000000000000000000000000000000")
  424. out32 [][32]byte
  425. out15 [][15]byte
  426. )
  427. // test 32
  428. err = abi.Unpack(&out32, "testDynamicFixedBytes32", marshalledReturn32)
  429. if err != nil {
  430. t.Fatal(err)
  431. }
  432. if len(out32) != 2 {
  433. t.Fatalf("expected array with 2 values, got %d", len(out32))
  434. }
  435. expected := common.Hex2Bytes("3078313233343536373839300000000000000000000000000000000000000000")
  436. if !bytes.Equal(out32[0][:], expected) {
  437. t.Errorf("expected %x, got %x\n", expected, out32[0])
  438. }
  439. expected = common.Hex2Bytes("3078303938373635343332310000000000000000000000000000000000000000")
  440. if !bytes.Equal(out32[1][:], expected) {
  441. t.Errorf("expected %x, got %x\n", expected, out32[1])
  442. }
  443. // test 15
  444. err = abi.Unpack(&out15, "testDynamicFixedBytes32", marshalledReturn15)
  445. if err != nil {
  446. t.Fatal(err)
  447. }
  448. if len(out15) != 2 {
  449. t.Fatalf("expected array with 2 values, got %d", len(out15))
  450. }
  451. expected = common.Hex2Bytes("307830313233343500000000000000")
  452. if !bytes.Equal(out15[0][:], expected) {
  453. t.Errorf("expected %x, got %x\n", expected, out15[0])
  454. }
  455. expected = common.Hex2Bytes("307839383736353400000000000000")
  456. if !bytes.Equal(out15[1][:], expected) {
  457. t.Errorf("expected %x, got %x\n", expected, out15[1])
  458. }
  459. }
  460. type methodMultiOutput struct {
  461. Int *big.Int
  462. String string
  463. }
  464. func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
  465. const definition = `[
  466. { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  467. var expected = methodMultiOutput{big.NewInt(1), "hello"}
  468. abi, err := JSON(strings.NewReader(definition))
  469. require.NoError(err)
  470. // using buff to make the code readable
  471. buff := new(bytes.Buffer)
  472. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  473. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  474. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  475. buff.Write(common.RightPadBytes([]byte(expected.String), 32))
  476. return abi, buff.Bytes(), expected
  477. }
  478. func TestMethodMultiReturn(t *testing.T) {
  479. type reversed struct {
  480. String string
  481. Int *big.Int
  482. }
  483. abi, data, expected := methodMultiReturn(require.New(t))
  484. bigint := new(big.Int)
  485. var testCases = []struct {
  486. dest interface{}
  487. expected interface{}
  488. error string
  489. name string
  490. }{{
  491. &methodMultiOutput{},
  492. &expected,
  493. "",
  494. "Can unpack into structure",
  495. }, {
  496. &reversed{},
  497. &reversed{expected.String, expected.Int},
  498. "",
  499. "Can unpack into reversed structure",
  500. }, {
  501. &[]interface{}{&bigint, new(string)},
  502. &[]interface{}{&expected.Int, &expected.String},
  503. "",
  504. "Can unpack into a slice",
  505. }, {
  506. &[2]interface{}{&bigint, new(string)},
  507. &[2]interface{}{&expected.Int, &expected.String},
  508. "",
  509. "Can unpack into an array",
  510. }, {
  511. &[]interface{}{new(int), new(int)},
  512. &[]interface{}{&expected.Int, &expected.String},
  513. "abi: cannot unmarshal *big.Int in to int",
  514. "Can not unpack into a slice with wrong types",
  515. }, {
  516. &[]interface{}{new(int)},
  517. &[]interface{}{},
  518. "abi: insufficient number of elements in the list/array for unpack, want 2, got 1",
  519. "Can not unpack into a slice with wrong types",
  520. }}
  521. for _, tc := range testCases {
  522. tc := tc
  523. t.Run(tc.name, func(t *testing.T) {
  524. require := require.New(t)
  525. err := abi.Unpack(tc.dest, "multi", data)
  526. if tc.error == "" {
  527. require.Nil(err, "Should be able to unpack method outputs.")
  528. require.Equal(tc.expected, tc.dest)
  529. } else {
  530. require.EqualError(err, tc.error)
  531. }
  532. })
  533. }
  534. }
  535. func TestMultiReturnWithArray(t *testing.T) {
  536. const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
  537. abi, err := JSON(strings.NewReader(definition))
  538. if err != nil {
  539. t.Fatal(err)
  540. }
  541. buff := new(bytes.Buffer)
  542. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
  543. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
  544. ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
  545. ret2, ret2Exp := new(uint64), uint64(8)
  546. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  547. t.Fatal(err)
  548. }
  549. if !reflect.DeepEqual(*ret1, ret1Exp) {
  550. t.Error("array result", *ret1, "!= Expected", ret1Exp)
  551. }
  552. if *ret2 != ret2Exp {
  553. t.Error("int result", *ret2, "!= Expected", ret2Exp)
  554. }
  555. }
  556. func TestMultiReturnWithStringArray(t *testing.T) {
  557. const definition = `[{"name" : "multi", "outputs": [{"name": "","type": "uint256[3]"},{"name": "","type": "address"},{"name": "","type": "string[2]"},{"name": "","type": "bool"}]}]`
  558. abi, err := JSON(strings.NewReader(definition))
  559. if err != nil {
  560. t.Fatal(err)
  561. }
  562. buff := new(bytes.Buffer)
  563. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"))
  564. temp, _ := big.NewInt(0).SetString("30000000000000000000", 10)
  565. ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp}
  566. ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f")
  567. ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"}
  568. ret4, ret4Exp := new(bool), false
  569. if err := abi.Unpack(&[]interface{}{ret1, ret2, ret3, ret4}, "multi", buff.Bytes()); err != nil {
  570. t.Fatal(err)
  571. }
  572. if !reflect.DeepEqual(*ret1, ret1Exp) {
  573. t.Error("big.Int array result", *ret1, "!= Expected", ret1Exp)
  574. }
  575. if !reflect.DeepEqual(*ret2, ret2Exp) {
  576. t.Error("address result", *ret2, "!= Expected", ret2Exp)
  577. }
  578. if !reflect.DeepEqual(*ret3, ret3Exp) {
  579. t.Error("string array result", *ret3, "!= Expected", ret3Exp)
  580. }
  581. if !reflect.DeepEqual(*ret4, ret4Exp) {
  582. t.Error("bool result", *ret4, "!= Expected", ret4Exp)
  583. }
  584. }
  585. func TestMultiReturnWithStringSlice(t *testing.T) {
  586. const definition = `[{"name" : "multi", "outputs": [{"name": "","type": "string[]"},{"name": "","type": "uint256[]"}]}]`
  587. abi, err := JSON(strings.NewReader(definition))
  588. if err != nil {
  589. t.Fatal(err)
  590. }
  591. buff := new(bytes.Buffer)
  592. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008657468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b676f2d657468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000065"))
  593. ret1, ret1Exp := new([]string), []string{"ethereum", "go-ethereum"}
  594. ret2, ret2Exp := new([]*big.Int), []*big.Int{big.NewInt(100), big.NewInt(101)}
  595. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  596. t.Fatal(err)
  597. }
  598. if !reflect.DeepEqual(*ret1, ret1Exp) {
  599. t.Error("string slice result", *ret1, "!= Expected", ret1Exp)
  600. }
  601. if !reflect.DeepEqual(*ret2, ret2Exp) {
  602. t.Error("uint256 slice result", *ret2, "!= Expected", ret2Exp)
  603. }
  604. }
  605. func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
  606. // Similar to TestMultiReturnWithArray, but with a special case in mind:
  607. // values of nested static arrays count towards the size as well, and any element following
  608. // after such nested array argument should be read with the correct offset,
  609. // so that it does not read content from the previous array argument.
  610. const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
  611. abi, err := JSON(strings.NewReader(definition))
  612. if err != nil {
  613. t.Fatal(err)
  614. }
  615. buff := new(bytes.Buffer)
  616. // construct the test array, each 3 char element is joined with 61 '0' chars,
  617. // to from the ((3 + 61) * 0.5) = 32 byte elements in the array.
  618. buff.Write(common.Hex2Bytes(strings.Join([]string{
  619. "", //empty, to apply the 61-char separator to the first element as well.
  620. "111", "112", "113", "121", "122", "123",
  621. "211", "212", "213", "221", "222", "223",
  622. "311", "312", "313", "321", "322", "323",
  623. "411", "412", "413", "421", "422", "423",
  624. }, "0000000000000000000000000000000000000000000000000000000000000")))
  625. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
  626. ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
  627. {{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
  628. {{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
  629. {{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
  630. {{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
  631. }
  632. ret2, ret2Exp := new(uint64), uint64(0x9876)
  633. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  634. t.Fatal(err)
  635. }
  636. if !reflect.DeepEqual(*ret1, ret1Exp) {
  637. t.Error("array result", *ret1, "!= Expected", ret1Exp)
  638. }
  639. if *ret2 != ret2Exp {
  640. t.Error("int result", *ret2, "!= Expected", ret2Exp)
  641. }
  642. }
  643. func TestUnmarshal(t *testing.T) {
  644. const definition = `[
  645. { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
  646. { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
  647. { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
  648. { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  649. { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  650. { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
  651. { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
  652. { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
  653. { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  654. abi, err := JSON(strings.NewReader(definition))
  655. if err != nil {
  656. t.Fatal(err)
  657. }
  658. buff := new(bytes.Buffer)
  659. // marshall mixed bytes (mixedBytes)
  660. p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
  661. p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
  662. mixedBytes := []interface{}{&p0, &p1}
  663. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  664. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
  665. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
  666. buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
  667. err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
  668. if err != nil {
  669. t.Error(err)
  670. } else {
  671. if !bytes.Equal(p0, p0Exp) {
  672. t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
  673. }
  674. if !bytes.Equal(p1[:], p1Exp) {
  675. t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
  676. }
  677. }
  678. // marshal int
  679. var Int *big.Int
  680. err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  681. if err != nil {
  682. t.Error(err)
  683. }
  684. if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
  685. t.Error("expected Int to be 1 got", Int)
  686. }
  687. // marshal bool
  688. var Bool bool
  689. err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  690. if err != nil {
  691. t.Error(err)
  692. }
  693. if !Bool {
  694. t.Error("expected Bool to be true")
  695. }
  696. // marshal dynamic bytes max length 32
  697. buff.Reset()
  698. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  699. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  700. bytesOut := common.RightPadBytes([]byte("hello"), 32)
  701. buff.Write(bytesOut)
  702. var Bytes []byte
  703. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  704. if err != nil {
  705. t.Error(err)
  706. }
  707. if !bytes.Equal(Bytes, bytesOut) {
  708. t.Errorf("expected %x got %x", bytesOut, Bytes)
  709. }
  710. // marshall dynamic bytes max length 64
  711. buff.Reset()
  712. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  713. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  714. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  715. buff.Write(bytesOut)
  716. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  717. if err != nil {
  718. t.Error(err)
  719. }
  720. if !bytes.Equal(Bytes, bytesOut) {
  721. t.Errorf("expected %x got %x", bytesOut, Bytes)
  722. }
  723. // marshall dynamic bytes max length 64
  724. buff.Reset()
  725. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  726. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  727. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  728. buff.Write(bytesOut)
  729. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  730. if err != nil {
  731. t.Error(err)
  732. }
  733. if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
  734. t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
  735. }
  736. // marshal dynamic bytes output empty
  737. err = abi.Unpack(&Bytes, "bytes", nil)
  738. if err == nil {
  739. t.Error("expected error")
  740. }
  741. // marshal dynamic bytes length 5
  742. buff.Reset()
  743. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  744. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  745. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  746. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  747. if err != nil {
  748. t.Error(err)
  749. }
  750. if !bytes.Equal(Bytes, []byte("hello")) {
  751. t.Errorf("expected %x got %x", bytesOut, Bytes)
  752. }
  753. // marshal dynamic bytes length 5
  754. buff.Reset()
  755. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  756. var hash common.Hash
  757. err = abi.Unpack(&hash, "fixed", buff.Bytes())
  758. if err != nil {
  759. t.Error(err)
  760. }
  761. helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  762. if hash != helloHash {
  763. t.Errorf("Expected %x to equal %x", hash, helloHash)
  764. }
  765. // marshal error
  766. buff.Reset()
  767. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  768. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  769. if err == nil {
  770. t.Error("expected error")
  771. }
  772. err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
  773. if err == nil {
  774. t.Error("expected error")
  775. }
  776. buff.Reset()
  777. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  778. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
  779. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
  780. // marshal int array
  781. var intArray [3]*big.Int
  782. err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
  783. if err != nil {
  784. t.Error(err)
  785. }
  786. var testAgainstIntArray [3]*big.Int
  787. testAgainstIntArray[0] = big.NewInt(1)
  788. testAgainstIntArray[1] = big.NewInt(2)
  789. testAgainstIntArray[2] = big.NewInt(3)
  790. for i, Int := range intArray {
  791. if Int.Cmp(testAgainstIntArray[i]) != 0 {
  792. t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
  793. }
  794. }
  795. // marshal address slice
  796. buff.Reset()
  797. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
  798. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  799. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  800. var outAddr []common.Address
  801. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  802. if err != nil {
  803. t.Fatal("didn't expect error:", err)
  804. }
  805. if len(outAddr) != 1 {
  806. t.Fatal("expected 1 item, got", len(outAddr))
  807. }
  808. if outAddr[0] != (common.Address{1}) {
  809. t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
  810. }
  811. // marshal multiple address slice
  812. buff.Reset()
  813. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
  814. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
  815. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  816. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  817. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
  818. buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
  819. buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
  820. var outAddrStruct struct {
  821. A []common.Address
  822. B []common.Address
  823. }
  824. err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
  825. if err != nil {
  826. t.Fatal("didn't expect error:", err)
  827. }
  828. if len(outAddrStruct.A) != 1 {
  829. t.Fatal("expected 1 item, got", len(outAddrStruct.A))
  830. }
  831. if outAddrStruct.A[0] != (common.Address{1}) {
  832. t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
  833. }
  834. if len(outAddrStruct.B) != 2 {
  835. t.Fatal("expected 1 item, got", len(outAddrStruct.B))
  836. }
  837. if outAddrStruct.B[0] != (common.Address{2}) {
  838. t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
  839. }
  840. if outAddrStruct.B[1] != (common.Address{3}) {
  841. t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
  842. }
  843. // marshal invalid address slice
  844. buff.Reset()
  845. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
  846. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  847. if err == nil {
  848. t.Fatal("expected error:", err)
  849. }
  850. }
  851. func TestOOMMaliciousInput(t *testing.T) {
  852. oomTests := []unpackTest{
  853. {
  854. def: `[{"type": "uint8[]"}]`,
  855. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  856. "0000000000000000000000000000000000000000000000000000000000000003" + // num elems
  857. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  858. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  859. },
  860. { // Length larger than 64 bits
  861. def: `[{"type": "uint8[]"}]`,
  862. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  863. "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
  864. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  865. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  866. },
  867. { // Offset very large (over 64 bits)
  868. def: `[{"type": "uint8[]"}]`,
  869. enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
  870. "0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  871. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  872. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  873. },
  874. { // Offset very large (below 64 bits)
  875. def: `[{"type": "uint8[]"}]`,
  876. enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
  877. "0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  878. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  879. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  880. },
  881. { // Offset negative (as 64 bit)
  882. def: `[{"type": "uint8[]"}]`,
  883. enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
  884. "0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  885. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  886. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  887. },
  888. { // Negative length
  889. def: `[{"type": "uint8[]"}]`,
  890. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  891. "000000000000000000000000000000000000000000000000f000000000000002" + // num elems
  892. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  893. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  894. },
  895. { // Very large length
  896. def: `[{"type": "uint8[]"}]`,
  897. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  898. "0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
  899. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  900. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  901. },
  902. }
  903. for i, test := range oomTests {
  904. def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  905. abi, err := JSON(strings.NewReader(def))
  906. if err != nil {
  907. t.Fatalf("invalid ABI definition %s: %v", def, err)
  908. }
  909. encb, err := hex.DecodeString(test.enc)
  910. if err != nil {
  911. t.Fatalf("invalid hex: %s" + test.enc)
  912. }
  913. _, err = abi.Methods["method"].Outputs.UnpackValues(encb)
  914. if err == nil {
  915. t.Fatalf("Expected error on malicious input, test %d", i)
  916. }
  917. }
  918. }