unpack_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. &[2]interface{}{&bigint, new(string)},
  329. &[2]interface{}{&expected.Int, &expected.String},
  330. "",
  331. "Can unpack into an array",
  332. }, {
  333. &[]interface{}{new(int), new(int)},
  334. &[]interface{}{&expected.Int, &expected.String},
  335. "abi: cannot unmarshal *big.Int in to int",
  336. "Can not unpack into a slice with wrong types",
  337. }, {
  338. &[]interface{}{new(int)},
  339. &[]interface{}{},
  340. "abi: insufficient number of elements in the list/array for unpack, want 2, got 1",
  341. "Can not unpack into a slice with wrong types",
  342. }}
  343. for _, tc := range testCases {
  344. tc := tc
  345. t.Run(tc.name, func(t *testing.T) {
  346. require := require.New(t)
  347. err := abi.Unpack(tc.dest, "multi", data)
  348. if tc.error == "" {
  349. require.Nil(err, "Should be able to unpack method outputs.")
  350. require.Equal(tc.expected, tc.dest)
  351. } else {
  352. require.EqualError(err, tc.error)
  353. }
  354. })
  355. }
  356. }
  357. func TestMultiReturnWithArray(t *testing.T) {
  358. const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
  359. abi, err := JSON(strings.NewReader(definition))
  360. if err != nil {
  361. t.Fatal(err)
  362. }
  363. buff := new(bytes.Buffer)
  364. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
  365. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
  366. ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
  367. ret2, ret2Exp := new(uint64), uint64(8)
  368. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  369. t.Fatal(err)
  370. }
  371. if !reflect.DeepEqual(*ret1, ret1Exp) {
  372. t.Error("array result", *ret1, "!= Expected", ret1Exp)
  373. }
  374. if *ret2 != ret2Exp {
  375. t.Error("int result", *ret2, "!= Expected", ret2Exp)
  376. }
  377. }
  378. func TestUnmarshal(t *testing.T) {
  379. const definition = `[
  380. { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
  381. { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
  382. { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
  383. { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  384. { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  385. { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
  386. { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
  387. { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
  388. { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  389. abi, err := JSON(strings.NewReader(definition))
  390. if err != nil {
  391. t.Fatal(err)
  392. }
  393. buff := new(bytes.Buffer)
  394. // marshall mixed bytes (mixedBytes)
  395. p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
  396. p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
  397. mixedBytes := []interface{}{&p0, &p1}
  398. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  399. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
  400. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
  401. buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
  402. err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
  403. if err != nil {
  404. t.Error(err)
  405. } else {
  406. if !bytes.Equal(p0, p0Exp) {
  407. t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
  408. }
  409. if !bytes.Equal(p1[:], p1Exp) {
  410. t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
  411. }
  412. }
  413. // marshal int
  414. var Int *big.Int
  415. err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  416. if err != nil {
  417. t.Error(err)
  418. }
  419. if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
  420. t.Error("expected Int to be 1 got", Int)
  421. }
  422. // marshal bool
  423. var Bool bool
  424. err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  425. if err != nil {
  426. t.Error(err)
  427. }
  428. if !Bool {
  429. t.Error("expected Bool to be true")
  430. }
  431. // marshal dynamic bytes max length 32
  432. buff.Reset()
  433. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  434. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  435. bytesOut := common.RightPadBytes([]byte("hello"), 32)
  436. buff.Write(bytesOut)
  437. var Bytes []byte
  438. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  439. if err != nil {
  440. t.Error(err)
  441. }
  442. if !bytes.Equal(Bytes, bytesOut) {
  443. t.Errorf("expected %x got %x", bytesOut, Bytes)
  444. }
  445. // marshall dynamic bytes max length 64
  446. buff.Reset()
  447. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  448. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  449. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  450. buff.Write(bytesOut)
  451. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  452. if err != nil {
  453. t.Error(err)
  454. }
  455. if !bytes.Equal(Bytes, bytesOut) {
  456. t.Errorf("expected %x got %x", bytesOut, Bytes)
  457. }
  458. // marshall dynamic bytes max length 64
  459. buff.Reset()
  460. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  461. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  462. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  463. buff.Write(bytesOut)
  464. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  465. if err != nil {
  466. t.Error(err)
  467. }
  468. if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
  469. t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
  470. }
  471. // marshal dynamic bytes output empty
  472. err = abi.Unpack(&Bytes, "bytes", nil)
  473. if err == nil {
  474. t.Error("expected error")
  475. }
  476. // marshal dynamic bytes length 5
  477. buff.Reset()
  478. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  479. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  480. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  481. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  482. if err != nil {
  483. t.Error(err)
  484. }
  485. if !bytes.Equal(Bytes, []byte("hello")) {
  486. t.Errorf("expected %x got %x", bytesOut, Bytes)
  487. }
  488. // marshal dynamic bytes length 5
  489. buff.Reset()
  490. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  491. var hash common.Hash
  492. err = abi.Unpack(&hash, "fixed", buff.Bytes())
  493. if err != nil {
  494. t.Error(err)
  495. }
  496. helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  497. if hash != helloHash {
  498. t.Errorf("Expected %x to equal %x", hash, helloHash)
  499. }
  500. // marshal error
  501. buff.Reset()
  502. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  503. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  504. if err == nil {
  505. t.Error("expected error")
  506. }
  507. err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
  508. if err == nil {
  509. t.Error("expected error")
  510. }
  511. buff.Reset()
  512. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  513. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
  514. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
  515. // marshal int array
  516. var intArray [3]*big.Int
  517. err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
  518. if err != nil {
  519. t.Error(err)
  520. }
  521. var testAgainstIntArray [3]*big.Int
  522. testAgainstIntArray[0] = big.NewInt(1)
  523. testAgainstIntArray[1] = big.NewInt(2)
  524. testAgainstIntArray[2] = big.NewInt(3)
  525. for i, Int := range intArray {
  526. if Int.Cmp(testAgainstIntArray[i]) != 0 {
  527. t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
  528. }
  529. }
  530. // marshal address slice
  531. buff.Reset()
  532. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
  533. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  534. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  535. var outAddr []common.Address
  536. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  537. if err != nil {
  538. t.Fatal("didn't expect error:", err)
  539. }
  540. if len(outAddr) != 1 {
  541. t.Fatal("expected 1 item, got", len(outAddr))
  542. }
  543. if outAddr[0] != (common.Address{1}) {
  544. t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
  545. }
  546. // marshal multiple address slice
  547. buff.Reset()
  548. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
  549. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
  550. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  551. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  552. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
  553. buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
  554. buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
  555. var outAddrStruct struct {
  556. A []common.Address
  557. B []common.Address
  558. }
  559. err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
  560. if err != nil {
  561. t.Fatal("didn't expect error:", err)
  562. }
  563. if len(outAddrStruct.A) != 1 {
  564. t.Fatal("expected 1 item, got", len(outAddrStruct.A))
  565. }
  566. if outAddrStruct.A[0] != (common.Address{1}) {
  567. t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
  568. }
  569. if len(outAddrStruct.B) != 2 {
  570. t.Fatal("expected 1 item, got", len(outAddrStruct.B))
  571. }
  572. if outAddrStruct.B[0] != (common.Address{2}) {
  573. t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
  574. }
  575. if outAddrStruct.B[1] != (common.Address{3}) {
  576. t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
  577. }
  578. // marshal invalid address slice
  579. buff.Reset()
  580. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
  581. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  582. if err == nil {
  583. t.Fatal("expected error:", err)
  584. }
  585. }