abi_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. "log"
  21. "math/big"
  22. "reflect"
  23. "strings"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. )
  28. const jsondata = `
  29. [
  30. { "type" : "function", "name" : "balance", "const" : true },
  31. { "type" : "function", "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
  32. ]`
  33. const jsondata2 = `
  34. [
  35. { "type" : "function", "name" : "balance", "const" : true },
  36. { "type" : "function", "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
  37. { "type" : "function", "name" : "test", "const" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
  38. { "type" : "function", "name" : "string", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
  39. { "type" : "function", "name" : "bool", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
  40. { "type" : "function", "name" : "address", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
  41. { "type" : "function", "name" : "string32", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string32" } ] },
  42. { "type" : "function", "name" : "uint64[2]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
  43. { "type" : "function", "name" : "uint64[]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
  44. { "type" : "function", "name" : "foo", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
  45. { "type" : "function", "name" : "bar", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
  46. { "type" : "function", "name" : "slice", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
  47. { "type" : "function", "name" : "slice256", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] }
  48. ]`
  49. func TestType(t *testing.T) {
  50. typ, err := NewType("uint32")
  51. if err != nil {
  52. t.Error(err)
  53. }
  54. if typ.Kind != reflect.Ptr {
  55. t.Error("expected uint32 to have kind Ptr")
  56. }
  57. typ, err = NewType("uint32[]")
  58. if err != nil {
  59. t.Error(err)
  60. }
  61. if typ.Kind != reflect.Slice {
  62. t.Error("expected uint32[] to have type slice")
  63. }
  64. if typ.Type != ubig_ts {
  65. t.Error("expcted uith32[] to have type uint64")
  66. }
  67. typ, err = NewType("uint32[2]")
  68. if err != nil {
  69. t.Error(err)
  70. }
  71. if typ.Kind != reflect.Slice {
  72. t.Error("expected uint32[2] to have kind slice")
  73. }
  74. if typ.Type != ubig_ts {
  75. t.Error("expcted uith32[2] to have type uint64")
  76. }
  77. if typ.Size != 2 {
  78. t.Error("expected uint32[2] to have a size of 2")
  79. }
  80. }
  81. func TestReader(t *testing.T) {
  82. Uint256, _ := NewType("uint256")
  83. exp := ABI{
  84. Methods: map[string]Method{
  85. "balance": Method{
  86. "balance", true, nil, nil,
  87. },
  88. "send": Method{
  89. "send", false, []Argument{
  90. Argument{"amount", Uint256, false},
  91. }, nil,
  92. },
  93. },
  94. }
  95. abi, err := JSON(strings.NewReader(jsondata))
  96. if err != nil {
  97. t.Error(err)
  98. }
  99. // deep equal fails for some reason
  100. t.Skip()
  101. if !reflect.DeepEqual(abi, exp) {
  102. t.Errorf("\nabi: %v\ndoes not match exp: %v", abi, exp)
  103. }
  104. }
  105. func TestTestNumbers(t *testing.T) {
  106. abi, err := JSON(strings.NewReader(jsondata2))
  107. if err != nil {
  108. t.Error(err)
  109. t.FailNow()
  110. }
  111. if _, err := abi.Pack("balance"); err != nil {
  112. t.Error(err)
  113. }
  114. if _, err := abi.Pack("balance", 1); err == nil {
  115. t.Error("expected error for balance(1)")
  116. }
  117. if _, err := abi.Pack("doesntexist", nil); err == nil {
  118. t.Errorf("doesntexist shouldn't exist")
  119. }
  120. if _, err := abi.Pack("doesntexist", 1); err == nil {
  121. t.Errorf("doesntexist(1) shouldn't exist")
  122. }
  123. if _, err := abi.Pack("send", big.NewInt(1000)); err != nil {
  124. t.Error(err)
  125. }
  126. i := new(int)
  127. *i = 1000
  128. if _, err := abi.Pack("send", i); err == nil {
  129. t.Errorf("expected send( ptr ) to throw, requires *big.Int instead of *int")
  130. }
  131. if _, err := abi.Pack("send", 1000); err != nil {
  132. t.Error("expected send(1000) to cast to big")
  133. }
  134. if _, err := abi.Pack("test", uint32(1000)); err != nil {
  135. t.Error(err)
  136. }
  137. }
  138. func TestTestString(t *testing.T) {
  139. abi, err := JSON(strings.NewReader(jsondata2))
  140. if err != nil {
  141. t.Error(err)
  142. t.FailNow()
  143. }
  144. if _, err := abi.Pack("string", "hello world"); err != nil {
  145. t.Error(err)
  146. }
  147. str10 := string(make([]byte, 10))
  148. if _, err := abi.Pack("string32", str10); err != nil {
  149. t.Error(err)
  150. }
  151. str32 := string(make([]byte, 32))
  152. if _, err := abi.Pack("string32", str32); err != nil {
  153. t.Error(err)
  154. }
  155. str33 := string(make([]byte, 33))
  156. if _, err := abi.Pack("string32", str33); err == nil {
  157. t.Error("expected str33 to throw out of bound error")
  158. }
  159. }
  160. func TestTestBool(t *testing.T) {
  161. abi, err := JSON(strings.NewReader(jsondata2))
  162. if err != nil {
  163. t.Error(err)
  164. t.FailNow()
  165. }
  166. if _, err := abi.Pack("bool", true); err != nil {
  167. t.Error(err)
  168. }
  169. }
  170. func TestTestSlice(t *testing.T) {
  171. abi, err := JSON(strings.NewReader(jsondata2))
  172. if err != nil {
  173. t.Error(err)
  174. t.FailNow()
  175. }
  176. addr := make([]byte, 20)
  177. if _, err := abi.Pack("address", addr); err != nil {
  178. t.Error(err)
  179. }
  180. addr = make([]byte, 21)
  181. if _, err := abi.Pack("address", addr); err == nil {
  182. t.Error("expected address of 21 width to throw")
  183. }
  184. slice := make([]byte, 2)
  185. if _, err := abi.Pack("uint64[2]", slice); err != nil {
  186. t.Error(err)
  187. }
  188. if _, err := abi.Pack("uint64[]", slice); err != nil {
  189. t.Error(err)
  190. }
  191. }
  192. func TestTestAddress(t *testing.T) {
  193. abi, err := JSON(strings.NewReader(jsondata2))
  194. if err != nil {
  195. t.Error(err)
  196. t.FailNow()
  197. }
  198. addr := make([]byte, 20)
  199. if _, err := abi.Pack("address", addr); err != nil {
  200. t.Error(err)
  201. }
  202. }
  203. func TestMethodSignature(t *testing.T) {
  204. String, _ := NewType("string")
  205. String32, _ := NewType("string32")
  206. m := Method{"foo", false, []Argument{Argument{"bar", String32, false}, Argument{"baz", String, false}}, nil}
  207. exp := "foo(string32,string)"
  208. if m.Sig() != exp {
  209. t.Error("signature mismatch", exp, "!=", m.Sig())
  210. }
  211. idexp := crypto.Keccak256([]byte(exp))[:4]
  212. if !bytes.Equal(m.Id(), idexp) {
  213. t.Errorf("expected ids to match %x != %x", m.Id(), idexp)
  214. }
  215. uintt, _ := NewType("uint")
  216. m = Method{"foo", false, []Argument{Argument{"bar", uintt, false}}, nil}
  217. exp = "foo(uint256)"
  218. if m.Sig() != exp {
  219. t.Error("signature mismatch", exp, "!=", m.Sig())
  220. }
  221. }
  222. func TestPack(t *testing.T) {
  223. abi, err := JSON(strings.NewReader(jsondata2))
  224. if err != nil {
  225. t.Error(err)
  226. t.FailNow()
  227. }
  228. sig := crypto.Keccak256([]byte("foo(uint32)"))[:4]
  229. sig = append(sig, make([]byte, 32)...)
  230. sig[35] = 10
  231. packed, err := abi.Pack("foo", uint32(10))
  232. if err != nil {
  233. t.Error(err)
  234. t.FailNow()
  235. }
  236. if !bytes.Equal(packed, sig) {
  237. t.Errorf("expected %x got %x", sig, packed)
  238. }
  239. }
  240. func TestMultiPack(t *testing.T) {
  241. abi, err := JSON(strings.NewReader(jsondata2))
  242. if err != nil {
  243. t.Error(err)
  244. t.FailNow()
  245. }
  246. sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4]
  247. sig = append(sig, make([]byte, 64)...)
  248. sig[35] = 10
  249. sig[67] = 11
  250. packed, err := abi.Pack("bar", uint32(10), uint16(11))
  251. if err != nil {
  252. t.Error(err)
  253. t.FailNow()
  254. }
  255. if !bytes.Equal(packed, sig) {
  256. t.Errorf("expected %x got %x", sig, packed)
  257. }
  258. }
  259. func TestPackSlice(t *testing.T) {
  260. abi, err := JSON(strings.NewReader(jsondata2))
  261. if err != nil {
  262. t.Error(err)
  263. t.FailNow()
  264. }
  265. sig := crypto.Keccak256([]byte("slice(uint32[2])"))[:4]
  266. sig = append(sig, make([]byte, 64)...)
  267. sig[35] = 1
  268. sig[67] = 2
  269. packed, err := abi.Pack("slice", []uint32{1, 2})
  270. if err != nil {
  271. t.Error(err)
  272. t.FailNow()
  273. }
  274. if !bytes.Equal(packed, sig) {
  275. t.Errorf("expected %x got %x", sig, packed)
  276. }
  277. }
  278. func TestPackSliceBig(t *testing.T) {
  279. abi, err := JSON(strings.NewReader(jsondata2))
  280. if err != nil {
  281. t.Error(err)
  282. t.FailNow()
  283. }
  284. sig := crypto.Keccak256([]byte("slice256(uint256[2])"))[:4]
  285. sig = append(sig, make([]byte, 64)...)
  286. sig[35] = 1
  287. sig[67] = 2
  288. packed, err := abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
  289. if err != nil {
  290. t.Error(err)
  291. t.FailNow()
  292. }
  293. if !bytes.Equal(packed, sig) {
  294. t.Errorf("expected %x got %x", sig, packed)
  295. }
  296. }
  297. func ExampleJSON() {
  298. const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]`
  299. abi, err := JSON(strings.NewReader(definition))
  300. if err != nil {
  301. log.Fatalln(err)
  302. }
  303. out, err := abi.Pack("isBar", common.HexToAddress("01"))
  304. if err != nil {
  305. log.Fatalln(err)
  306. }
  307. fmt.Printf("%x\n", out)
  308. // Output:
  309. // 1f2c40920000000000000000000000000000000000000000000000000000000000000001
  310. }
  311. func TestBytes(t *testing.T) {
  312. const definition = `[
  313. { "type" : "function", "name" : "balance", "const" : true, "inputs" : [ { "name" : "address", "type" : "bytes20" } ] },
  314. { "type" : "function", "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
  315. ]`
  316. abi, err := JSON(strings.NewReader(definition))
  317. if err != nil {
  318. t.Fatal(err)
  319. }
  320. ok := make([]byte, 20)
  321. _, err = abi.Pack("balance", ok)
  322. if err != nil {
  323. t.Error(err)
  324. }
  325. toosmall := make([]byte, 19)
  326. _, err = abi.Pack("balance", toosmall)
  327. if err != nil {
  328. t.Error(err)
  329. }
  330. toobig := make([]byte, 21)
  331. _, err = abi.Pack("balance", toobig)
  332. if err == nil {
  333. t.Error("expected error")
  334. }
  335. }
  336. func TestDefaultFunctionParsing(t *testing.T) {
  337. const definition = `[{ "name" : "balance" }]`
  338. abi, err := JSON(strings.NewReader(definition))
  339. if err != nil {
  340. t.Fatal(err)
  341. }
  342. if _, ok := abi.Methods["balance"]; !ok {
  343. t.Error("expected 'balance' to be present")
  344. }
  345. }
  346. func TestBareEvents(t *testing.T) {
  347. const definition = `[
  348. { "type" : "event", "name" : "balance" },
  349. { "type" : "event", "name" : "name" }]`
  350. abi, err := JSON(strings.NewReader(definition))
  351. if err != nil {
  352. t.Fatal(err)
  353. }
  354. if len(abi.Events) != 2 {
  355. t.Error("expected 2 events")
  356. }
  357. if _, ok := abi.Events["balance"]; !ok {
  358. t.Error("expected 'balance' event to be present")
  359. }
  360. if _, ok := abi.Events["name"]; !ok {
  361. t.Error("expected 'name' event to be present")
  362. }
  363. }
  364. func TestMultiReturnWithStruct(t *testing.T) {
  365. const definition = `[
  366. { "name" : "multi", "const" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  367. abi, err := JSON(strings.NewReader(definition))
  368. if err != nil {
  369. t.Fatal(err)
  370. }
  371. // using buff to make the code readable
  372. buff := new(bytes.Buffer)
  373. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  374. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  375. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  376. stringOut := "hello"
  377. buff.Write(common.RightPadBytes([]byte(stringOut), 32))
  378. var inter struct {
  379. Int *big.Int
  380. String string
  381. }
  382. err = abi.unmarshal(&inter, "multi", buff.Bytes())
  383. if err != nil {
  384. t.Error(err)
  385. }
  386. if inter.Int == nil || inter.Int.Cmp(big.NewInt(1)) != 0 {
  387. t.Error("expected Int to be 1 got", inter.Int)
  388. }
  389. if inter.String != stringOut {
  390. t.Error("expected String to be", stringOut, "got", inter.String)
  391. }
  392. var reversed struct {
  393. String string
  394. Int *big.Int
  395. }
  396. err = abi.unmarshal(&reversed, "multi", buff.Bytes())
  397. if err != nil {
  398. t.Error(err)
  399. }
  400. if reversed.Int == nil || reversed.Int.Cmp(big.NewInt(1)) != 0 {
  401. t.Error("expected Int to be 1 got", reversed.Int)
  402. }
  403. if reversed.String != stringOut {
  404. t.Error("expected String to be", stringOut, "got", reversed.String)
  405. }
  406. }
  407. func TestMultiReturnWithSlice(t *testing.T) {
  408. const definition = `[
  409. { "name" : "multi", "const" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  410. abi, err := JSON(strings.NewReader(definition))
  411. if err != nil {
  412. t.Fatal(err)
  413. }
  414. // using buff to make the code readable
  415. buff := new(bytes.Buffer)
  416. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  417. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  418. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  419. stringOut := "hello"
  420. buff.Write(common.RightPadBytes([]byte(stringOut), 32))
  421. var inter []interface{}
  422. err = abi.unmarshal(&inter, "multi", buff.Bytes())
  423. if err != nil {
  424. t.Error(err)
  425. }
  426. if len(inter) != 2 {
  427. t.Fatal("expected 2 results got", len(inter))
  428. }
  429. if num, ok := inter[0].(*big.Int); !ok || num.Cmp(big.NewInt(1)) != 0 {
  430. t.Error("expected index 0 to be 1 got", num)
  431. }
  432. if str, ok := inter[1].(string); !ok || str != stringOut {
  433. t.Error("expected index 1 to be", stringOut, "got", str)
  434. }
  435. }
  436. func TestMarshalArrays(t *testing.T) {
  437. const definition = `[
  438. { "name" : "bytes32", "const" : false, "outputs": [ { "type": "bytes32" } ] },
  439. { "name" : "bytes10", "const" : false, "outputs": [ { "type": "bytes10" } ] }
  440. ]`
  441. abi, err := JSON(strings.NewReader(definition))
  442. if err != nil {
  443. t.Fatal(err)
  444. }
  445. output := common.LeftPadBytes([]byte{1}, 32)
  446. var bytes10 [10]byte
  447. err = abi.unmarshal(&bytes10, "bytes32", output)
  448. if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
  449. t.Error("expected error or bytes32 not be assignable to bytes10:", err)
  450. }
  451. var bytes32 [32]byte
  452. err = abi.unmarshal(&bytes32, "bytes32", output)
  453. if err != nil {
  454. t.Error("didn't expect error:", err)
  455. }
  456. if !bytes.Equal(bytes32[:], output) {
  457. t.Error("expected bytes32[31] to be 1 got", bytes32[31])
  458. }
  459. type (
  460. B10 [10]byte
  461. B32 [32]byte
  462. )
  463. var b10 B10
  464. err = abi.unmarshal(&b10, "bytes32", output)
  465. if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
  466. t.Error("expected error or bytes32 not be assignable to bytes10:", err)
  467. }
  468. var b32 B32
  469. err = abi.unmarshal(&b32, "bytes32", output)
  470. if err != nil {
  471. t.Error("didn't expect error:", err)
  472. }
  473. if !bytes.Equal(b32[:], output) {
  474. t.Error("expected bytes32[31] to be 1 got", bytes32[31])
  475. }
  476. output[10] = 1
  477. var shortAssignLong [32]byte
  478. err = abi.unmarshal(&shortAssignLong, "bytes10", output)
  479. if err != nil {
  480. t.Error("didn't expect error:", err)
  481. }
  482. if !bytes.Equal(output, shortAssignLong[:]) {
  483. t.Errorf("expected %x to be %x", shortAssignLong, output)
  484. }
  485. }
  486. func TestUnmarshal(t *testing.T) {
  487. const definition = `[
  488. { "name" : "int", "const" : false, "outputs": [ { "type": "uint256" } ] },
  489. { "name" : "bool", "const" : false, "outputs": [ { "type": "bool" } ] },
  490. { "name" : "bytes", "const" : false, "outputs": [ { "type": "bytes" } ] },
  491. { "name" : "fixed", "const" : false, "outputs": [ { "type": "bytes32" } ] },
  492. { "name" : "multi", "const" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  493. { "name" : "mixedBytes", "const" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  494. abi, err := JSON(strings.NewReader(definition))
  495. if err != nil {
  496. t.Fatal(err)
  497. }
  498. // marshal int
  499. var Int *big.Int
  500. err = abi.unmarshal(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  501. if err != nil {
  502. t.Error(err)
  503. }
  504. if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
  505. t.Error("expected Int to be 1 got", Int)
  506. }
  507. // marshal bool
  508. var Bool bool
  509. err = abi.unmarshal(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  510. if err != nil {
  511. t.Error(err)
  512. }
  513. if !Bool {
  514. t.Error("expected Bool to be true")
  515. }
  516. // marshal dynamic bytes max length 32
  517. buff := new(bytes.Buffer)
  518. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  519. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  520. bytesOut := common.RightPadBytes([]byte("hello"), 32)
  521. buff.Write(bytesOut)
  522. var Bytes []byte
  523. err = abi.unmarshal(&Bytes, "bytes", buff.Bytes())
  524. if err != nil {
  525. t.Error(err)
  526. }
  527. if !bytes.Equal(Bytes, bytesOut) {
  528. t.Errorf("expected %x got %x", bytesOut, Bytes)
  529. }
  530. // marshall dynamic bytes max length 64
  531. buff.Reset()
  532. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  533. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  534. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  535. buff.Write(bytesOut)
  536. err = abi.unmarshal(&Bytes, "bytes", buff.Bytes())
  537. if err != nil {
  538. t.Error(err)
  539. }
  540. if !bytes.Equal(Bytes, bytesOut) {
  541. t.Errorf("expected %x got %x", bytesOut, Bytes)
  542. }
  543. // marshall dynamic bytes max length 63
  544. buff.Reset()
  545. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  546. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  547. bytesOut = common.RightPadBytes([]byte("hello"), 63)
  548. buff.Write(bytesOut)
  549. err = abi.unmarshal(&Bytes, "bytes", buff.Bytes())
  550. if err != nil {
  551. t.Error(err)
  552. }
  553. if !bytes.Equal(Bytes, bytesOut) {
  554. t.Errorf("expected %x got %x", bytesOut, Bytes)
  555. }
  556. // marshal dynamic bytes output empty
  557. err = abi.unmarshal(&Bytes, "bytes", nil)
  558. if err == nil {
  559. t.Error("expected error")
  560. }
  561. // marshal dynamic bytes length 5
  562. buff.Reset()
  563. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  564. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  565. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  566. err = abi.unmarshal(&Bytes, "bytes", buff.Bytes())
  567. if err != nil {
  568. t.Error(err)
  569. }
  570. if !bytes.Equal(Bytes, []byte("hello")) {
  571. t.Errorf("expected %x got %x", bytesOut, Bytes)
  572. }
  573. // marshal dynamic bytes length 5
  574. buff.Reset()
  575. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  576. var hash common.Hash
  577. err = abi.unmarshal(&hash, "fixed", buff.Bytes())
  578. if err != nil {
  579. t.Error(err)
  580. }
  581. helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  582. if hash != helloHash {
  583. t.Errorf("Expected %x to equal %x", hash, helloHash)
  584. }
  585. // marshal error
  586. buff.Reset()
  587. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  588. err = abi.unmarshal(&Bytes, "bytes", buff.Bytes())
  589. if err == nil {
  590. t.Error("expected error")
  591. }
  592. err = abi.unmarshal(&Bytes, "multi", make([]byte, 64))
  593. if err == nil {
  594. t.Error("expected error")
  595. }
  596. // marshal mixed bytes
  597. buff.Reset()
  598. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  599. fixed := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")
  600. buff.Write(fixed)
  601. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  602. bytesOut = common.RightPadBytes([]byte("hello"), 32)
  603. buff.Write(bytesOut)
  604. var out []interface{}
  605. err = abi.unmarshal(&out, "mixedBytes", buff.Bytes())
  606. if err != nil {
  607. t.Fatal("didn't expect error:", err)
  608. }
  609. if !bytes.Equal(bytesOut, out[0].([]byte)) {
  610. t.Errorf("expected %x, got %x", bytesOut, out[0])
  611. }
  612. if !bytes.Equal(fixed, out[1].([]byte)) {
  613. t.Errorf("expected %x, got %x", fixed, out[1])
  614. }
  615. }