unpack_test.go 20 KB

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