unpack_test.go 34 KB

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