unpack_test.go 23 KB

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