unpack_test.go 33 KB

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