unpack_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. )
  28. type unpackTest struct {
  29. def string // ABI definition JSON
  30. enc string // evm return data
  31. want interface{} // the expected output
  32. err string // empty or error if expected
  33. }
  34. func (test unpackTest) checkError(err error) error {
  35. if err != nil {
  36. if len(test.err) == 0 {
  37. return fmt.Errorf("expected no err but got: %v", err)
  38. } else if err.Error() != test.err {
  39. return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
  40. }
  41. } else if len(test.err) > 0 {
  42. return fmt.Errorf("expected err: %v but got none", test.err)
  43. }
  44. return nil
  45. }
  46. var unpackTests = []unpackTest{
  47. {
  48. def: `[{ "type": "bool" }]`,
  49. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  50. want: true,
  51. },
  52. {
  53. def: `[{"type": "uint32"}]`,
  54. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  55. want: uint32(1),
  56. },
  57. {
  58. def: `[{"type": "uint32"}]`,
  59. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  60. want: uint16(0),
  61. err: "abi: cannot unmarshal uint32 in to uint16",
  62. },
  63. {
  64. def: `[{"type": "uint17"}]`,
  65. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  66. want: uint16(0),
  67. err: "abi: cannot unmarshal *big.Int in to uint16",
  68. },
  69. {
  70. def: `[{"type": "uint17"}]`,
  71. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  72. want: big.NewInt(1),
  73. },
  74. {
  75. def: `[{"type": "int32"}]`,
  76. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  77. want: int32(1),
  78. },
  79. {
  80. def: `[{"type": "int32"}]`,
  81. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  82. want: int16(0),
  83. err: "abi: cannot unmarshal int32 in to int16",
  84. },
  85. {
  86. def: `[{"type": "int17"}]`,
  87. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  88. want: int16(0),
  89. err: "abi: cannot unmarshal *big.Int in to int16",
  90. },
  91. {
  92. def: `[{"type": "int17"}]`,
  93. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  94. want: big.NewInt(1),
  95. },
  96. {
  97. def: `[{"type": "address"}]`,
  98. enc: "0000000000000000000000000100000000000000000000000000000000000000",
  99. want: common.Address{1},
  100. },
  101. {
  102. def: `[{"type": "bytes32"}]`,
  103. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  104. 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},
  105. },
  106. {
  107. def: `[{"type": "bytes"}]`,
  108. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  109. want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  110. },
  111. {
  112. def: `[{"type": "bytes"}]`,
  113. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  114. want: [32]byte{},
  115. err: "abi: cannot unmarshal []uint8 in to [32]uint8",
  116. },
  117. {
  118. def: `[{"type": "bytes32"}]`,
  119. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  120. want: []byte(nil),
  121. err: "abi: cannot unmarshal [32]uint8 in to []uint8",
  122. },
  123. {
  124. def: `[{"type": "bytes32"}]`,
  125. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  126. want: common.HexToHash("0100000000000000000000000000000000000000000000000000000000000000"),
  127. },
  128. {
  129. def: `[{"type": "function"}]`,
  130. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  131. want: [24]byte{1},
  132. },
  133. // slices
  134. {
  135. def: `[{"type": "uint8[]"}]`,
  136. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  137. want: []uint8{1, 2},
  138. },
  139. {
  140. def: `[{"type": "uint8[2]"}]`,
  141. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  142. want: [2]uint8{1, 2},
  143. },
  144. // multi dimensional, if these pass, all types that don't require length prefix should pass
  145. {
  146. def: `[{"type": "uint8[][]"}]`,
  147. enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000E0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  148. want: [][]uint8{{1, 2}, {1, 2}},
  149. },
  150. {
  151. def: `[{"type": "uint8[2][2]"}]`,
  152. enc: "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  153. want: [2][2]uint8{{1, 2}, {1, 2}},
  154. },
  155. {
  156. def: `[{"type": "uint8[][2]"}]`,
  157. enc: "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
  158. want: [2][]uint8{{1}, {1}},
  159. },
  160. {
  161. def: `[{"type": "uint8[2][]"}]`,
  162. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  163. want: [][2]uint8{{1, 2}},
  164. },
  165. {
  166. def: `[{"type": "uint16[]"}]`,
  167. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  168. want: []uint16{1, 2},
  169. },
  170. {
  171. def: `[{"type": "uint16[2]"}]`,
  172. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  173. want: [2]uint16{1, 2},
  174. },
  175. {
  176. def: `[{"type": "uint32[]"}]`,
  177. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  178. want: []uint32{1, 2},
  179. },
  180. {
  181. def: `[{"type": "uint32[2]"}]`,
  182. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  183. want: [2]uint32{1, 2},
  184. },
  185. {
  186. def: `[{"type": "uint64[]"}]`,
  187. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  188. want: []uint64{1, 2},
  189. },
  190. {
  191. def: `[{"type": "uint64[2]"}]`,
  192. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  193. want: [2]uint64{1, 2},
  194. },
  195. {
  196. def: `[{"type": "uint256[]"}]`,
  197. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  198. want: []*big.Int{big.NewInt(1), big.NewInt(2)},
  199. },
  200. {
  201. def: `[{"type": "uint256[3]"}]`,
  202. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  203. want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  204. },
  205. {
  206. def: `[{"type": "int8[]"}]`,
  207. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  208. want: []int8{1, 2},
  209. },
  210. {
  211. def: `[{"type": "int8[2]"}]`,
  212. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  213. want: [2]int8{1, 2},
  214. },
  215. {
  216. def: `[{"type": "int16[]"}]`,
  217. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  218. want: []int16{1, 2},
  219. },
  220. {
  221. def: `[{"type": "int16[2]"}]`,
  222. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  223. want: [2]int16{1, 2},
  224. },
  225. {
  226. def: `[{"type": "int32[]"}]`,
  227. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  228. want: []int32{1, 2},
  229. },
  230. {
  231. def: `[{"type": "int32[2]"}]`,
  232. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  233. want: [2]int32{1, 2},
  234. },
  235. {
  236. def: `[{"type": "int64[]"}]`,
  237. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  238. want: []int64{1, 2},
  239. },
  240. {
  241. def: `[{"type": "int64[2]"}]`,
  242. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  243. want: [2]int64{1, 2},
  244. },
  245. {
  246. def: `[{"type": "int256[]"}]`,
  247. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  248. want: []*big.Int{big.NewInt(1), big.NewInt(2)},
  249. },
  250. {
  251. def: `[{"type": "int256[3]"}]`,
  252. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  253. want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  254. },
  255. }
  256. func TestUnpack(t *testing.T) {
  257. for i, test := range unpackTests {
  258. t.Run(strconv.Itoa(i), func(t *testing.T) {
  259. def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  260. abi, err := JSON(strings.NewReader(def))
  261. if err != nil {
  262. t.Fatalf("invalid ABI definition %s: %v", def, err)
  263. }
  264. encb, err := hex.DecodeString(test.enc)
  265. if err != nil {
  266. t.Fatalf("invalid hex: %s" + test.enc)
  267. }
  268. outptr := reflect.New(reflect.TypeOf(test.want))
  269. err = abi.Unpack(outptr.Interface(), "method", encb)
  270. if err := test.checkError(err); err != nil {
  271. t.Errorf("test %d (%v) failed: %v", i, test.def, err)
  272. return
  273. }
  274. out := outptr.Elem().Interface()
  275. if !reflect.DeepEqual(test.want, out) {
  276. t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
  277. }
  278. })
  279. }
  280. }
  281. func TestMultiReturnWithStruct(t *testing.T) {
  282. const definition = `[
  283. { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  284. abi, err := JSON(strings.NewReader(definition))
  285. if err != nil {
  286. t.Fatal(err)
  287. }
  288. // using buff to make the code readable
  289. buff := new(bytes.Buffer)
  290. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  291. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  292. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  293. stringOut := "hello"
  294. buff.Write(common.RightPadBytes([]byte(stringOut), 32))
  295. var inter struct {
  296. Int *big.Int
  297. String string
  298. }
  299. err = abi.Unpack(&inter, "multi", buff.Bytes())
  300. if err != nil {
  301. t.Error(err)
  302. }
  303. if inter.Int == nil || inter.Int.Cmp(big.NewInt(1)) != 0 {
  304. t.Error("expected Int to be 1 got", inter.Int)
  305. }
  306. if inter.String != stringOut {
  307. t.Error("expected String to be", stringOut, "got", inter.String)
  308. }
  309. var reversed struct {
  310. String string
  311. Int *big.Int
  312. }
  313. err = abi.Unpack(&reversed, "multi", buff.Bytes())
  314. if err != nil {
  315. t.Error(err)
  316. }
  317. if reversed.Int == nil || reversed.Int.Cmp(big.NewInt(1)) != 0 {
  318. t.Error("expected Int to be 1 got", reversed.Int)
  319. }
  320. if reversed.String != stringOut {
  321. t.Error("expected String to be", stringOut, "got", reversed.String)
  322. }
  323. }
  324. func TestMultiReturnWithArray(t *testing.T) {
  325. const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
  326. abi, err := JSON(strings.NewReader(definition))
  327. if err != nil {
  328. t.Fatal(err)
  329. }
  330. buff := new(bytes.Buffer)
  331. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
  332. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
  333. ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
  334. ret2, ret2Exp := new(uint64), uint64(8)
  335. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  336. t.Fatal(err)
  337. }
  338. if !reflect.DeepEqual(*ret1, ret1Exp) {
  339. t.Error("array result", *ret1, "!= Expected", ret1Exp)
  340. }
  341. if *ret2 != ret2Exp {
  342. t.Error("int result", *ret2, "!= Expected", ret2Exp)
  343. }
  344. }
  345. func TestUnmarshal(t *testing.T) {
  346. const definition = `[
  347. { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
  348. { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
  349. { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
  350. { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  351. { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  352. { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
  353. { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
  354. { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
  355. { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  356. abi, err := JSON(strings.NewReader(definition))
  357. if err != nil {
  358. t.Fatal(err)
  359. }
  360. buff := new(bytes.Buffer)
  361. // marshall mixed bytes (mixedBytes)
  362. p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
  363. p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
  364. mixedBytes := []interface{}{&p0, &p1}
  365. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  366. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
  367. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
  368. buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
  369. err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
  370. if err != nil {
  371. t.Error(err)
  372. } else {
  373. if !bytes.Equal(p0, p0Exp) {
  374. t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
  375. }
  376. if !bytes.Equal(p1[:], p1Exp) {
  377. t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
  378. }
  379. }
  380. // marshal int
  381. var Int *big.Int
  382. err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  383. if err != nil {
  384. t.Error(err)
  385. }
  386. if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
  387. t.Error("expected Int to be 1 got", Int)
  388. }
  389. // marshal bool
  390. var Bool bool
  391. err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  392. if err != nil {
  393. t.Error(err)
  394. }
  395. if !Bool {
  396. t.Error("expected Bool to be true")
  397. }
  398. // marshal dynamic bytes max length 32
  399. buff.Reset()
  400. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  401. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  402. bytesOut := common.RightPadBytes([]byte("hello"), 32)
  403. buff.Write(bytesOut)
  404. var Bytes []byte
  405. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  406. if err != nil {
  407. t.Error(err)
  408. }
  409. if !bytes.Equal(Bytes, bytesOut) {
  410. t.Errorf("expected %x got %x", bytesOut, Bytes)
  411. }
  412. // marshall dynamic bytes max length 64
  413. buff.Reset()
  414. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  415. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  416. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  417. buff.Write(bytesOut)
  418. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  419. if err != nil {
  420. t.Error(err)
  421. }
  422. if !bytes.Equal(Bytes, bytesOut) {
  423. t.Errorf("expected %x got %x", bytesOut, Bytes)
  424. }
  425. // marshall dynamic bytes max length 64
  426. buff.Reset()
  427. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  428. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  429. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  430. buff.Write(bytesOut)
  431. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  432. if err != nil {
  433. t.Error(err)
  434. }
  435. if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
  436. t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
  437. }
  438. // marshal dynamic bytes output empty
  439. err = abi.Unpack(&Bytes, "bytes", nil)
  440. if err == nil {
  441. t.Error("expected error")
  442. }
  443. // marshal dynamic bytes length 5
  444. buff.Reset()
  445. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  446. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  447. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  448. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  449. if err != nil {
  450. t.Error(err)
  451. }
  452. if !bytes.Equal(Bytes, []byte("hello")) {
  453. t.Errorf("expected %x got %x", bytesOut, Bytes)
  454. }
  455. // marshal dynamic bytes length 5
  456. buff.Reset()
  457. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  458. var hash common.Hash
  459. err = abi.Unpack(&hash, "fixed", buff.Bytes())
  460. if err != nil {
  461. t.Error(err)
  462. }
  463. helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  464. if hash != helloHash {
  465. t.Errorf("Expected %x to equal %x", hash, helloHash)
  466. }
  467. // marshal error
  468. buff.Reset()
  469. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  470. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  471. if err == nil {
  472. t.Error("expected error")
  473. }
  474. err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
  475. if err == nil {
  476. t.Error("expected error")
  477. }
  478. buff.Reset()
  479. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  480. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
  481. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
  482. // marshal int array
  483. var intArray [3]*big.Int
  484. err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
  485. if err != nil {
  486. t.Error(err)
  487. }
  488. var testAgainstIntArray [3]*big.Int
  489. testAgainstIntArray[0] = big.NewInt(1)
  490. testAgainstIntArray[1] = big.NewInt(2)
  491. testAgainstIntArray[2] = big.NewInt(3)
  492. for i, Int := range intArray {
  493. if Int.Cmp(testAgainstIntArray[i]) != 0 {
  494. t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
  495. }
  496. }
  497. // marshal address slice
  498. buff.Reset()
  499. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
  500. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  501. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  502. var outAddr []common.Address
  503. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  504. if err != nil {
  505. t.Fatal("didn't expect error:", err)
  506. }
  507. if len(outAddr) != 1 {
  508. t.Fatal("expected 1 item, got", len(outAddr))
  509. }
  510. if outAddr[0] != (common.Address{1}) {
  511. t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
  512. }
  513. // marshal multiple address slice
  514. buff.Reset()
  515. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
  516. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
  517. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  518. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  519. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
  520. buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
  521. buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
  522. var outAddrStruct struct {
  523. A []common.Address
  524. B []common.Address
  525. }
  526. err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
  527. if err != nil {
  528. t.Fatal("didn't expect error:", err)
  529. }
  530. if len(outAddrStruct.A) != 1 {
  531. t.Fatal("expected 1 item, got", len(outAddrStruct.A))
  532. }
  533. if outAddrStruct.A[0] != (common.Address{1}) {
  534. t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
  535. }
  536. if len(outAddrStruct.B) != 2 {
  537. t.Fatal("expected 1 item, got", len(outAddrStruct.B))
  538. }
  539. if outAddrStruct.B[0] != (common.Address{2}) {
  540. t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
  541. }
  542. if outAddrStruct.B[1] != (common.Address{3}) {
  543. t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
  544. }
  545. // marshal invalid address slice
  546. buff.Reset()
  547. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
  548. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  549. if err == nil {
  550. t.Fatal("expected error:", err)
  551. }
  552. }