unpack_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. // Copyright 2015 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: common.HexToHash("0100000000000000000000000000000000000000000000000000000000000000"),
  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": "uint64[]"}]`,
  188. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  189. want: []uint64{1, 2},
  190. },
  191. {
  192. def: `[{"type": "uint64[2]"}]`,
  193. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  194. want: [2]uint64{1, 2},
  195. },
  196. {
  197. def: `[{"type": "uint256[]"}]`,
  198. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  199. want: []*big.Int{big.NewInt(1), big.NewInt(2)},
  200. },
  201. {
  202. def: `[{"type": "uint256[3]"}]`,
  203. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  204. want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  205. },
  206. {
  207. def: `[{"type": "int8[]"}]`,
  208. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  209. want: []int8{1, 2},
  210. },
  211. {
  212. def: `[{"type": "int8[2]"}]`,
  213. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  214. want: [2]int8{1, 2},
  215. },
  216. {
  217. def: `[{"type": "int16[]"}]`,
  218. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  219. want: []int16{1, 2},
  220. },
  221. {
  222. def: `[{"type": "int16[2]"}]`,
  223. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  224. want: [2]int16{1, 2},
  225. },
  226. {
  227. def: `[{"type": "int32[]"}]`,
  228. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  229. want: []int32{1, 2},
  230. },
  231. {
  232. def: `[{"type": "int32[2]"}]`,
  233. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  234. want: [2]int32{1, 2},
  235. },
  236. {
  237. def: `[{"type": "int64[]"}]`,
  238. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  239. want: []int64{1, 2},
  240. },
  241. {
  242. def: `[{"type": "int64[2]"}]`,
  243. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  244. want: [2]int64{1, 2},
  245. },
  246. {
  247. def: `[{"type": "int256[]"}]`,
  248. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  249. want: []*big.Int{big.NewInt(1), big.NewInt(2)},
  250. },
  251. {
  252. def: `[{"type": "int256[3]"}]`,
  253. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  254. want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  255. },
  256. }
  257. func TestUnpack(t *testing.T) {
  258. for i, test := range unpackTests {
  259. t.Run(strconv.Itoa(i), func(t *testing.T) {
  260. def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  261. abi, err := JSON(strings.NewReader(def))
  262. if err != nil {
  263. t.Fatalf("invalid ABI definition %s: %v", def, err)
  264. }
  265. encb, err := hex.DecodeString(test.enc)
  266. if err != nil {
  267. t.Fatalf("invalid hex: %s" + test.enc)
  268. }
  269. outptr := reflect.New(reflect.TypeOf(test.want))
  270. err = abi.Unpack(outptr.Interface(), "method", encb)
  271. if err := test.checkError(err); err != nil {
  272. t.Errorf("test %d (%v) failed: %v", i, test.def, err)
  273. return
  274. }
  275. out := outptr.Elem().Interface()
  276. if !reflect.DeepEqual(test.want, out) {
  277. t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
  278. }
  279. })
  280. }
  281. }
  282. type methodMultiOutput struct {
  283. Int *big.Int
  284. String string
  285. }
  286. func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
  287. const definition = `[
  288. { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  289. var expected = methodMultiOutput{big.NewInt(1), "hello"}
  290. abi, err := JSON(strings.NewReader(definition))
  291. require.NoError(err)
  292. // using buff to make the code readable
  293. buff := new(bytes.Buffer)
  294. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  295. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  296. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  297. buff.Write(common.RightPadBytes([]byte(expected.String), 32))
  298. return abi, buff.Bytes(), expected
  299. }
  300. func TestMethodMultiReturn(t *testing.T) {
  301. type reversed struct {
  302. String string
  303. Int *big.Int
  304. }
  305. abi, data, expected := methodMultiReturn(require.New(t))
  306. bigint := new(big.Int)
  307. var testCases = []struct {
  308. dest interface{}
  309. expected interface{}
  310. error string
  311. name string
  312. }{{
  313. &methodMultiOutput{},
  314. &expected,
  315. "",
  316. "Can unpack into structure",
  317. }, {
  318. &reversed{},
  319. &reversed{expected.String, expected.Int},
  320. "",
  321. "Can unpack into reversed structure",
  322. }, {
  323. &[]interface{}{&bigint, new(string)},
  324. &[]interface{}{&expected.Int, &expected.String},
  325. "",
  326. "Can unpack into a slice",
  327. }, {
  328. &[]interface{}{new(int), new(int)},
  329. &[]interface{}{&expected.Int, &expected.String},
  330. "abi: cannot unmarshal *big.Int in to int",
  331. "Can not unpack into a slice with wrong types",
  332. }, {
  333. &[]interface{}{new(int)},
  334. &[]interface{}{},
  335. "abi: insufficient number of elements in the list/array for unpack, want 2, got 1",
  336. "Can not unpack into a slice with wrong types",
  337. }}
  338. for _, tc := range testCases {
  339. tc := tc
  340. t.Run(tc.name, func(t *testing.T) {
  341. require := require.New(t)
  342. err := abi.Unpack(tc.dest, "multi", data)
  343. if tc.error == "" {
  344. require.Nil(err, "Should be able to unpack method outputs.")
  345. require.Equal(tc.expected, tc.dest)
  346. } else {
  347. require.EqualError(err, tc.error)
  348. }
  349. })
  350. }
  351. }
  352. func TestMultiReturnWithArray(t *testing.T) {
  353. const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
  354. abi, err := JSON(strings.NewReader(definition))
  355. if err != nil {
  356. t.Fatal(err)
  357. }
  358. buff := new(bytes.Buffer)
  359. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
  360. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
  361. ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
  362. ret2, ret2Exp := new(uint64), uint64(8)
  363. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  364. t.Fatal(err)
  365. }
  366. if !reflect.DeepEqual(*ret1, ret1Exp) {
  367. t.Error("array result", *ret1, "!= Expected", ret1Exp)
  368. }
  369. if *ret2 != ret2Exp {
  370. t.Error("int result", *ret2, "!= Expected", ret2Exp)
  371. }
  372. }
  373. func TestUnmarshal(t *testing.T) {
  374. const definition = `[
  375. { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
  376. { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
  377. { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
  378. { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  379. { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  380. { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
  381. { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
  382. { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
  383. { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  384. abi, err := JSON(strings.NewReader(definition))
  385. if err != nil {
  386. t.Fatal(err)
  387. }
  388. buff := new(bytes.Buffer)
  389. // marshall mixed bytes (mixedBytes)
  390. p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
  391. p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
  392. mixedBytes := []interface{}{&p0, &p1}
  393. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  394. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
  395. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
  396. buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
  397. err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
  398. if err != nil {
  399. t.Error(err)
  400. } else {
  401. if !bytes.Equal(p0, p0Exp) {
  402. t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
  403. }
  404. if !bytes.Equal(p1[:], p1Exp) {
  405. t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
  406. }
  407. }
  408. // marshal int
  409. var Int *big.Int
  410. err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  411. if err != nil {
  412. t.Error(err)
  413. }
  414. if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
  415. t.Error("expected Int to be 1 got", Int)
  416. }
  417. // marshal bool
  418. var Bool bool
  419. err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  420. if err != nil {
  421. t.Error(err)
  422. }
  423. if !Bool {
  424. t.Error("expected Bool to be true")
  425. }
  426. // marshal dynamic bytes max length 32
  427. buff.Reset()
  428. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  429. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  430. bytesOut := common.RightPadBytes([]byte("hello"), 32)
  431. buff.Write(bytesOut)
  432. var Bytes []byte
  433. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  434. if err != nil {
  435. t.Error(err)
  436. }
  437. if !bytes.Equal(Bytes, bytesOut) {
  438. t.Errorf("expected %x got %x", bytesOut, Bytes)
  439. }
  440. // marshall dynamic bytes max length 64
  441. buff.Reset()
  442. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  443. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  444. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  445. buff.Write(bytesOut)
  446. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  447. if err != nil {
  448. t.Error(err)
  449. }
  450. if !bytes.Equal(Bytes, bytesOut) {
  451. t.Errorf("expected %x got %x", bytesOut, Bytes)
  452. }
  453. // marshall dynamic bytes max length 64
  454. buff.Reset()
  455. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  456. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  457. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  458. buff.Write(bytesOut)
  459. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  460. if err != nil {
  461. t.Error(err)
  462. }
  463. if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
  464. t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
  465. }
  466. // marshal dynamic bytes output empty
  467. err = abi.Unpack(&Bytes, "bytes", nil)
  468. if err == nil {
  469. t.Error("expected error")
  470. }
  471. // marshal dynamic bytes length 5
  472. buff.Reset()
  473. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  474. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  475. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  476. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  477. if err != nil {
  478. t.Error(err)
  479. }
  480. if !bytes.Equal(Bytes, []byte("hello")) {
  481. t.Errorf("expected %x got %x", bytesOut, Bytes)
  482. }
  483. // marshal dynamic bytes length 5
  484. buff.Reset()
  485. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  486. var hash common.Hash
  487. err = abi.Unpack(&hash, "fixed", buff.Bytes())
  488. if err != nil {
  489. t.Error(err)
  490. }
  491. helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  492. if hash != helloHash {
  493. t.Errorf("Expected %x to equal %x", hash, helloHash)
  494. }
  495. // marshal error
  496. buff.Reset()
  497. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  498. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  499. if err == nil {
  500. t.Error("expected error")
  501. }
  502. err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
  503. if err == nil {
  504. t.Error("expected error")
  505. }
  506. buff.Reset()
  507. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  508. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
  509. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
  510. // marshal int array
  511. var intArray [3]*big.Int
  512. err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
  513. if err != nil {
  514. t.Error(err)
  515. }
  516. var testAgainstIntArray [3]*big.Int
  517. testAgainstIntArray[0] = big.NewInt(1)
  518. testAgainstIntArray[1] = big.NewInt(2)
  519. testAgainstIntArray[2] = big.NewInt(3)
  520. for i, Int := range intArray {
  521. if Int.Cmp(testAgainstIntArray[i]) != 0 {
  522. t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
  523. }
  524. }
  525. // marshal address slice
  526. buff.Reset()
  527. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
  528. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  529. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  530. var outAddr []common.Address
  531. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  532. if err != nil {
  533. t.Fatal("didn't expect error:", err)
  534. }
  535. if len(outAddr) != 1 {
  536. t.Fatal("expected 1 item, got", len(outAddr))
  537. }
  538. if outAddr[0] != (common.Address{1}) {
  539. t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
  540. }
  541. // marshal multiple address slice
  542. buff.Reset()
  543. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
  544. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
  545. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  546. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  547. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
  548. buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
  549. buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
  550. var outAddrStruct struct {
  551. A []common.Address
  552. B []common.Address
  553. }
  554. err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
  555. if err != nil {
  556. t.Fatal("didn't expect error:", err)
  557. }
  558. if len(outAddrStruct.A) != 1 {
  559. t.Fatal("expected 1 item, got", len(outAddrStruct.A))
  560. }
  561. if outAddrStruct.A[0] != (common.Address{1}) {
  562. t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
  563. }
  564. if len(outAddrStruct.B) != 2 {
  565. t.Fatal("expected 1 item, got", len(outAddrStruct.B))
  566. }
  567. if outAddrStruct.B[0] != (common.Address{2}) {
  568. t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
  569. }
  570. if outAddrStruct.B[1] != (common.Address{3}) {
  571. t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
  572. }
  573. // marshal invalid address slice
  574. buff.Reset()
  575. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
  576. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  577. if err == nil {
  578. t.Fatal("expected error:", err)
  579. }
  580. }