abi_test.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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. // formatSilceOutput add padding to the value and adds a size
  29. func formatSliceOutput(v ...[]byte) []byte {
  30. off := common.LeftPadBytes(big.NewInt(int64(len(v))).Bytes(), 32)
  31. output := append(off, make([]byte, 0, len(v)*32)...)
  32. for _, value := range v {
  33. output = append(output, common.LeftPadBytes(value, 32)...)
  34. }
  35. return output
  36. }
  37. // quick helper padding
  38. func pad(input []byte, size int, left bool) []byte {
  39. if left {
  40. return common.LeftPadBytes(input, size)
  41. }
  42. return common.RightPadBytes(input, size)
  43. }
  44. func TestTypeCheck(t *testing.T) {
  45. for i, test := range []struct {
  46. typ string
  47. input interface{}
  48. err string
  49. }{
  50. {"uint", big.NewInt(1), ""},
  51. {"int", big.NewInt(1), ""},
  52. {"uint30", big.NewInt(1), ""},
  53. {"uint30", uint8(1), "abi: cannot use uint8 as type ptr as argument"},
  54. {"uint16", uint16(1), ""},
  55. {"uint16", uint8(1), "abi: cannot use uint8 as type uint16 as argument"},
  56. {"uint16[]", []uint16{1, 2, 3}, ""},
  57. {"uint16[]", [3]uint16{1, 2, 3}, ""},
  58. {"uint16[]", []uint32{1, 2, 3}, "abi: cannot use []uint32 as type []uint16 as argument"},
  59. {"uint16[3]", [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"},
  60. {"uint16[3]", [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
  61. {"uint16[3]", []uint16{1, 2, 3}, ""},
  62. {"uint16[3]", []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
  63. {"address[]", []common.Address{common.Address{1}}, ""},
  64. {"address[1]", []common.Address{common.Address{1}}, ""},
  65. {"address[1]", [1]common.Address{common.Address{1}}, ""},
  66. {"address[2]", [1]common.Address{common.Address{1}}, "abi: cannot use [1]array as type [2]array as argument"},
  67. {"bytes32", [32]byte{}, ""},
  68. {"bytes32", [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"},
  69. {"bytes32", common.Hash{1}, ""},
  70. {"bytes31", [31]byte{}, ""},
  71. {"bytes31", [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"},
  72. {"bytes", []byte{0, 1}, ""},
  73. {"bytes", [2]byte{0, 1}, ""},
  74. {"bytes", common.Hash{1}, ""},
  75. {"string", "hello world", ""},
  76. {"bytes32[]", [][32]byte{[32]byte{}}, ""},
  77. } {
  78. typ, err := NewType(test.typ)
  79. if err != nil {
  80. t.Fatal("unexpected parse error:", err)
  81. }
  82. err = typeCheck(typ, reflect.ValueOf(test.input))
  83. if err != nil && len(test.err) == 0 {
  84. t.Errorf("%d failed. Expected no err but got: %v", i, err)
  85. continue
  86. }
  87. if err == nil && len(test.err) != 0 {
  88. t.Errorf("%d failed. Expected err: %v but got none", i, test.err)
  89. continue
  90. }
  91. if err != nil && len(test.err) != 0 && err.Error() != test.err {
  92. t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
  93. }
  94. }
  95. }
  96. func TestSimpleMethodUnpack(t *testing.T) {
  97. for i, test := range []struct {
  98. def string // definition of the **output** ABI params
  99. marshalledOutput []byte // evm return data
  100. expectedOut interface{} // the expected output
  101. outVar string // the output variable (e.g. uint32, *big.Int, etc)
  102. err string // empty or error if expected
  103. }{
  104. {
  105. `[ { "type": "uint32" } ]`,
  106. pad([]byte{1}, 32, true),
  107. uint32(1),
  108. "uint32",
  109. "",
  110. },
  111. {
  112. `[ { "type": "uint32" } ]`,
  113. pad([]byte{1}, 32, true),
  114. nil,
  115. "uint16",
  116. "abi: cannot unmarshal uint32 in to uint16",
  117. },
  118. {
  119. `[ { "type": "uint17" } ]`,
  120. pad([]byte{1}, 32, true),
  121. nil,
  122. "uint16",
  123. "abi: cannot unmarshal *big.Int in to uint16",
  124. },
  125. {
  126. `[ { "type": "uint17" } ]`,
  127. pad([]byte{1}, 32, true),
  128. big.NewInt(1),
  129. "*big.Int",
  130. "",
  131. },
  132. {
  133. `[ { "type": "int32" } ]`,
  134. pad([]byte{1}, 32, true),
  135. int32(1),
  136. "int32",
  137. "",
  138. },
  139. {
  140. `[ { "type": "int32" } ]`,
  141. pad([]byte{1}, 32, true),
  142. nil,
  143. "int16",
  144. "abi: cannot unmarshal int32 in to int16",
  145. },
  146. {
  147. `[ { "type": "int17" } ]`,
  148. pad([]byte{1}, 32, true),
  149. nil,
  150. "int16",
  151. "abi: cannot unmarshal *big.Int in to int16",
  152. },
  153. {
  154. `[ { "type": "int17" } ]`,
  155. pad([]byte{1}, 32, true),
  156. big.NewInt(1),
  157. "*big.Int",
  158. "",
  159. },
  160. {
  161. `[ { "type": "address" } ]`,
  162. pad(pad([]byte{1}, 20, false), 32, true),
  163. common.Address{1},
  164. "address",
  165. "",
  166. },
  167. {
  168. `[ { "type": "bytes32" } ]`,
  169. pad([]byte{1}, 32, false),
  170. pad([]byte{1}, 32, false),
  171. "bytes",
  172. "",
  173. },
  174. {
  175. `[ { "type": "bytes32" } ]`,
  176. pad([]byte{1}, 32, false),
  177. pad([]byte{1}, 32, false),
  178. "hash",
  179. "",
  180. },
  181. {
  182. `[ { "type": "bytes32" } ]`,
  183. pad([]byte{1}, 32, false),
  184. pad([]byte{1}, 32, false),
  185. "interface",
  186. "",
  187. },
  188. } {
  189. abiDefinition := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  190. abi, err := JSON(strings.NewReader(abiDefinition))
  191. if err != nil {
  192. t.Errorf("%d failed. %v", i, err)
  193. continue
  194. }
  195. var outvar interface{}
  196. switch test.outVar {
  197. case "uint8":
  198. var v uint8
  199. err = abi.Unpack(&v, "method", test.marshalledOutput)
  200. outvar = v
  201. case "uint16":
  202. var v uint16
  203. err = abi.Unpack(&v, "method", test.marshalledOutput)
  204. outvar = v
  205. case "uint32":
  206. var v uint32
  207. err = abi.Unpack(&v, "method", test.marshalledOutput)
  208. outvar = v
  209. case "uint64":
  210. var v uint64
  211. err = abi.Unpack(&v, "method", test.marshalledOutput)
  212. outvar = v
  213. case "int8":
  214. var v int8
  215. err = abi.Unpack(&v, "method", test.marshalledOutput)
  216. outvar = v
  217. case "int16":
  218. var v int16
  219. err = abi.Unpack(&v, "method", test.marshalledOutput)
  220. outvar = v
  221. case "int32":
  222. var v int32
  223. err = abi.Unpack(&v, "method", test.marshalledOutput)
  224. outvar = v
  225. case "int64":
  226. var v int64
  227. err = abi.Unpack(&v, "method", test.marshalledOutput)
  228. outvar = v
  229. case "*big.Int":
  230. var v *big.Int
  231. err = abi.Unpack(&v, "method", test.marshalledOutput)
  232. outvar = v
  233. case "address":
  234. var v common.Address
  235. err = abi.Unpack(&v, "method", test.marshalledOutput)
  236. outvar = v
  237. case "bytes":
  238. var v []byte
  239. err = abi.Unpack(&v, "method", test.marshalledOutput)
  240. outvar = v
  241. case "hash":
  242. var v common.Hash
  243. err = abi.Unpack(&v, "method", test.marshalledOutput)
  244. outvar = v
  245. case "interface":
  246. err = abi.Unpack(&outvar, "method", test.marshalledOutput)
  247. default:
  248. t.Errorf("unsupported type '%v' please add it to the switch statement in this test", test.outVar)
  249. continue
  250. }
  251. if err != nil && len(test.err) == 0 {
  252. t.Errorf("%d failed. Expected no err but got: %v", i, err)
  253. continue
  254. }
  255. if err == nil && len(test.err) != 0 {
  256. t.Errorf("%d failed. Expected err: %v but got none", i, test.err)
  257. continue
  258. }
  259. if err != nil && len(test.err) != 0 && err.Error() != test.err {
  260. t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
  261. continue
  262. }
  263. if err == nil {
  264. // bit of an ugly hack for hash type but I don't feel like finding a proper solution
  265. if test.outVar == "hash" {
  266. tmp := outvar.(common.Hash) // without assignment it's unaddressable
  267. outvar = tmp[:]
  268. }
  269. if !reflect.DeepEqual(test.expectedOut, outvar) {
  270. t.Errorf("%d failed. Output error: expected %v, got %v", i, test.expectedOut, outvar)
  271. }
  272. }
  273. }
  274. }
  275. func TestUnpackSetInterfaceSlice(t *testing.T) {
  276. var (
  277. var1 = new(uint8)
  278. var2 = new(uint8)
  279. )
  280. out := []interface{}{var1, var2}
  281. abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint8"}, {"type":"uint8"}]}]`))
  282. if err != nil {
  283. t.Fatal(err)
  284. }
  285. marshalledReturn := append(pad([]byte{1}, 32, true), pad([]byte{2}, 32, true)...)
  286. err = abi.Unpack(&out, "ints", marshalledReturn)
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. if *var1 != 1 {
  291. t.Error("expected var1 to be 1, got", *var1)
  292. }
  293. if *var2 != 2 {
  294. t.Error("expected var2 to be 2, got", *var2)
  295. }
  296. out = []interface{}{var1}
  297. err = abi.Unpack(&out, "ints", marshalledReturn)
  298. expErr := "abi: cannot marshal in to slices of unequal size (require: 2, got: 1)"
  299. if err == nil || err.Error() != expErr {
  300. t.Error("expected err:", expErr, "Got:", err)
  301. }
  302. }
  303. func TestPack(t *testing.T) {
  304. for i, test := range []struct {
  305. typ string
  306. input interface{}
  307. output []byte
  308. }{
  309. {"uint16", uint16(2), pad([]byte{2}, 32, true)},
  310. {"uint16[]", []uint16{1, 2}, formatSliceOutput([]byte{1}, []byte{2})},
  311. {"bytes20", [20]byte{1}, pad([]byte{1}, 32, false)},
  312. {"uint256[]", []*big.Int{big.NewInt(1), big.NewInt(2)}, formatSliceOutput([]byte{1}, []byte{2})},
  313. {"address[]", []common.Address{common.Address{1}, common.Address{2}}, formatSliceOutput(pad([]byte{1}, 20, false), pad([]byte{2}, 20, false))},
  314. {"bytes32[]", []common.Hash{common.Hash{1}, common.Hash{2}}, formatSliceOutput(pad([]byte{1}, 32, false), pad([]byte{2}, 32, false))},
  315. } {
  316. typ, err := NewType(test.typ)
  317. if err != nil {
  318. t.Fatal("unexpected parse error:", err)
  319. }
  320. output, err := typ.pack(reflect.ValueOf(test.input))
  321. if err != nil {
  322. t.Fatal("unexpected pack error:", err)
  323. }
  324. if !bytes.Equal(output, test.output) {
  325. t.Errorf("%d failed. Expected bytes: '%x' Got: '%x'", i, test.output, output)
  326. }
  327. }
  328. }
  329. func TestMethodPack(t *testing.T) {
  330. abi, err := JSON(strings.NewReader(jsondata2))
  331. if err != nil {
  332. t.Fatal(err)
  333. }
  334. sig := abi.Methods["slice"].Id()
  335. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  336. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  337. packed, err := abi.Pack("slice", []uint32{1, 2})
  338. if err != nil {
  339. t.Error(err)
  340. }
  341. if !bytes.Equal(packed, sig) {
  342. t.Errorf("expected %x got %x", sig, packed)
  343. }
  344. var addrA, addrB = common.Address{1}, common.Address{2}
  345. sig = abi.Methods["sliceAddress"].Id()
  346. sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
  347. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  348. sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
  349. sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
  350. packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
  351. if err != nil {
  352. t.Fatal(err)
  353. }
  354. if !bytes.Equal(packed, sig) {
  355. t.Errorf("expected %x got %x", sig, packed)
  356. }
  357. var addrC, addrD = common.Address{3}, common.Address{4}
  358. sig = abi.Methods["sliceMultiAddress"].Id()
  359. sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
  360. sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
  361. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  362. sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
  363. sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
  364. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  365. sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
  366. sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
  367. packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
  368. if err != nil {
  369. t.Fatal(err)
  370. }
  371. if !bytes.Equal(packed, sig) {
  372. t.Errorf("expected %x got %x", sig, packed)
  373. }
  374. sig = abi.Methods["slice256"].Id()
  375. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  376. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  377. packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
  378. if err != nil {
  379. t.Error(err)
  380. }
  381. if !bytes.Equal(packed, sig) {
  382. t.Errorf("expected %x got %x", sig, packed)
  383. }
  384. }
  385. const jsondata = `
  386. [
  387. { "type" : "function", "name" : "balance", "constant" : true },
  388. { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
  389. ]`
  390. const jsondata2 = `
  391. [
  392. { "type" : "function", "name" : "balance", "constant" : true },
  393. { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
  394. { "type" : "function", "name" : "test", "constant" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
  395. { "type" : "function", "name" : "string", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
  396. { "type" : "function", "name" : "bool", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
  397. { "type" : "function", "name" : "address", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
  398. { "type" : "function", "name" : "uint64[2]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
  399. { "type" : "function", "name" : "uint64[]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
  400. { "type" : "function", "name" : "foo", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
  401. { "type" : "function", "name" : "bar", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
  402. { "type" : "function", "name" : "slice", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
  403. { "type" : "function", "name" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] },
  404. { "type" : "function", "name" : "sliceAddress", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] },
  405. { "type" : "function", "name" : "sliceMultiAddress", "constant" : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] }
  406. ]`
  407. func TestReader(t *testing.T) {
  408. Uint256, _ := NewType("uint256")
  409. exp := ABI{
  410. Methods: map[string]Method{
  411. "balance": Method{
  412. "balance", true, nil, nil,
  413. },
  414. "send": Method{
  415. "send", false, []Argument{
  416. Argument{"amount", Uint256, false},
  417. }, nil,
  418. },
  419. },
  420. }
  421. abi, err := JSON(strings.NewReader(jsondata))
  422. if err != nil {
  423. t.Error(err)
  424. }
  425. // deep equal fails for some reason
  426. t.Skip()
  427. if !reflect.DeepEqual(abi, exp) {
  428. t.Errorf("\nabi: %v\ndoes not match exp: %v", abi, exp)
  429. }
  430. }
  431. func TestTestNumbers(t *testing.T) {
  432. abi, err := JSON(strings.NewReader(jsondata2))
  433. if err != nil {
  434. t.Error(err)
  435. t.FailNow()
  436. }
  437. if _, err := abi.Pack("balance"); err != nil {
  438. t.Error(err)
  439. }
  440. if _, err := abi.Pack("balance", 1); err == nil {
  441. t.Error("expected error for balance(1)")
  442. }
  443. if _, err := abi.Pack("doesntexist", nil); err == nil {
  444. t.Errorf("doesntexist shouldn't exist")
  445. }
  446. if _, err := abi.Pack("doesntexist", 1); err == nil {
  447. t.Errorf("doesntexist(1) shouldn't exist")
  448. }
  449. if _, err := abi.Pack("send", big.NewInt(1000)); err != nil {
  450. t.Error(err)
  451. }
  452. i := new(int)
  453. *i = 1000
  454. if _, err := abi.Pack("send", i); err == nil {
  455. t.Errorf("expected send( ptr ) to throw, requires *big.Int instead of *int")
  456. }
  457. if _, err := abi.Pack("test", uint32(1000)); err != nil {
  458. t.Error(err)
  459. }
  460. }
  461. func TestTestString(t *testing.T) {
  462. abi, err := JSON(strings.NewReader(jsondata2))
  463. if err != nil {
  464. t.Error(err)
  465. t.FailNow()
  466. }
  467. if _, err := abi.Pack("string", "hello world"); err != nil {
  468. t.Error(err)
  469. }
  470. }
  471. func TestTestBool(t *testing.T) {
  472. abi, err := JSON(strings.NewReader(jsondata2))
  473. if err != nil {
  474. t.Error(err)
  475. t.FailNow()
  476. }
  477. if _, err := abi.Pack("bool", true); err != nil {
  478. t.Error(err)
  479. }
  480. }
  481. func TestTestSlice(t *testing.T) {
  482. abi, err := JSON(strings.NewReader(jsondata2))
  483. if err != nil {
  484. t.Error(err)
  485. t.FailNow()
  486. }
  487. slice := make([]uint64, 2)
  488. if _, err := abi.Pack("uint64[2]", slice); err != nil {
  489. t.Error(err)
  490. }
  491. if _, err := abi.Pack("uint64[]", slice); err != nil {
  492. t.Error(err)
  493. }
  494. }
  495. func TestMethodSignature(t *testing.T) {
  496. String, _ := NewType("string")
  497. m := Method{"foo", false, []Argument{Argument{"bar", String, false}, Argument{"baz", String, false}}, nil}
  498. exp := "foo(string,string)"
  499. if m.Sig() != exp {
  500. t.Error("signature mismatch", exp, "!=", m.Sig())
  501. }
  502. idexp := crypto.Keccak256([]byte(exp))[:4]
  503. if !bytes.Equal(m.Id(), idexp) {
  504. t.Errorf("expected ids to match %x != %x", m.Id(), idexp)
  505. }
  506. uintt, _ := NewType("uint")
  507. m = Method{"foo", false, []Argument{Argument{"bar", uintt, false}}, nil}
  508. exp = "foo(uint256)"
  509. if m.Sig() != exp {
  510. t.Error("signature mismatch", exp, "!=", m.Sig())
  511. }
  512. }
  513. func TestMultiPack(t *testing.T) {
  514. abi, err := JSON(strings.NewReader(jsondata2))
  515. if err != nil {
  516. t.Error(err)
  517. t.FailNow()
  518. }
  519. sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4]
  520. sig = append(sig, make([]byte, 64)...)
  521. sig[35] = 10
  522. sig[67] = 11
  523. packed, err := abi.Pack("bar", uint32(10), uint16(11))
  524. if err != nil {
  525. t.Error(err)
  526. t.FailNow()
  527. }
  528. if !bytes.Equal(packed, sig) {
  529. t.Errorf("expected %x got %x", sig, packed)
  530. }
  531. }
  532. func ExampleJSON() {
  533. const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]`
  534. abi, err := JSON(strings.NewReader(definition))
  535. if err != nil {
  536. log.Fatalln(err)
  537. }
  538. out, err := abi.Pack("isBar", common.HexToAddress("01"))
  539. if err != nil {
  540. log.Fatalln(err)
  541. }
  542. fmt.Printf("%x\n", out)
  543. // Output:
  544. // 1f2c40920000000000000000000000000000000000000000000000000000000000000001
  545. }
  546. func TestInputVariableInputLength(t *testing.T) {
  547. const definition = `[
  548. { "type" : "function", "name" : "strOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] },
  549. { "type" : "function", "name" : "bytesOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] },
  550. { "type" : "function", "name" : "strTwo", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] }
  551. ]`
  552. abi, err := JSON(strings.NewReader(definition))
  553. if err != nil {
  554. t.Fatal(err)
  555. }
  556. // test one string
  557. strin := "hello world"
  558. strpack, err := abi.Pack("strOne", strin)
  559. if err != nil {
  560. t.Error(err)
  561. }
  562. offset := make([]byte, 32)
  563. offset[31] = 32
  564. length := make([]byte, 32)
  565. length[31] = byte(len(strin))
  566. value := common.RightPadBytes([]byte(strin), 32)
  567. exp := append(offset, append(length, value...)...)
  568. // ignore first 4 bytes of the output. This is the function identifier
  569. strpack = strpack[4:]
  570. if !bytes.Equal(strpack, exp) {
  571. t.Errorf("expected %x, got %x\n", exp, strpack)
  572. }
  573. // test one bytes
  574. btspack, err := abi.Pack("bytesOne", []byte(strin))
  575. if err != nil {
  576. t.Error(err)
  577. }
  578. // ignore first 4 bytes of the output. This is the function identifier
  579. btspack = btspack[4:]
  580. if !bytes.Equal(btspack, exp) {
  581. t.Errorf("expected %x, got %x\n", exp, btspack)
  582. }
  583. // test two strings
  584. str1 := "hello"
  585. str2 := "world"
  586. str2pack, err := abi.Pack("strTwo", str1, str2)
  587. if err != nil {
  588. t.Error(err)
  589. }
  590. offset1 := make([]byte, 32)
  591. offset1[31] = 64
  592. length1 := make([]byte, 32)
  593. length1[31] = byte(len(str1))
  594. value1 := common.RightPadBytes([]byte(str1), 32)
  595. offset2 := make([]byte, 32)
  596. offset2[31] = 128
  597. length2 := make([]byte, 32)
  598. length2[31] = byte(len(str2))
  599. value2 := common.RightPadBytes([]byte(str2), 32)
  600. exp2 := append(offset1, offset2...)
  601. exp2 = append(exp2, append(length1, value1...)...)
  602. exp2 = append(exp2, append(length2, value2...)...)
  603. // ignore first 4 bytes of the output. This is the function identifier
  604. str2pack = str2pack[4:]
  605. if !bytes.Equal(str2pack, exp2) {
  606. t.Errorf("expected %x, got %x\n", exp, str2pack)
  607. }
  608. // test two strings, first > 32, second < 32
  609. str1 = strings.Repeat("a", 33)
  610. str2pack, err = abi.Pack("strTwo", str1, str2)
  611. if err != nil {
  612. t.Error(err)
  613. }
  614. offset1 = make([]byte, 32)
  615. offset1[31] = 64
  616. length1 = make([]byte, 32)
  617. length1[31] = byte(len(str1))
  618. value1 = common.RightPadBytes([]byte(str1), 64)
  619. offset2[31] = 160
  620. exp2 = append(offset1, offset2...)
  621. exp2 = append(exp2, append(length1, value1...)...)
  622. exp2 = append(exp2, append(length2, value2...)...)
  623. // ignore first 4 bytes of the output. This is the function identifier
  624. str2pack = str2pack[4:]
  625. if !bytes.Equal(str2pack, exp2) {
  626. t.Errorf("expected %x, got %x\n", exp, str2pack)
  627. }
  628. // test two strings, first > 32, second >32
  629. str1 = strings.Repeat("a", 33)
  630. str2 = strings.Repeat("a", 33)
  631. str2pack, err = abi.Pack("strTwo", str1, str2)
  632. if err != nil {
  633. t.Error(err)
  634. }
  635. offset1 = make([]byte, 32)
  636. offset1[31] = 64
  637. length1 = make([]byte, 32)
  638. length1[31] = byte(len(str1))
  639. value1 = common.RightPadBytes([]byte(str1), 64)
  640. offset2 = make([]byte, 32)
  641. offset2[31] = 160
  642. length2 = make([]byte, 32)
  643. length2[31] = byte(len(str2))
  644. value2 = common.RightPadBytes([]byte(str2), 64)
  645. exp2 = append(offset1, offset2...)
  646. exp2 = append(exp2, append(length1, value1...)...)
  647. exp2 = append(exp2, append(length2, value2...)...)
  648. // ignore first 4 bytes of the output. This is the function identifier
  649. str2pack = str2pack[4:]
  650. if !bytes.Equal(str2pack, exp2) {
  651. t.Errorf("expected %x, got %x\n", exp, str2pack)
  652. }
  653. }
  654. func TestDefaultFunctionParsing(t *testing.T) {
  655. const definition = `[{ "name" : "balance" }]`
  656. abi, err := JSON(strings.NewReader(definition))
  657. if err != nil {
  658. t.Fatal(err)
  659. }
  660. if _, ok := abi.Methods["balance"]; !ok {
  661. t.Error("expected 'balance' to be present")
  662. }
  663. }
  664. func TestBareEvents(t *testing.T) {
  665. const definition = `[
  666. { "type" : "event", "name" : "balance" },
  667. { "type" : "event", "name" : "name" }]`
  668. abi, err := JSON(strings.NewReader(definition))
  669. if err != nil {
  670. t.Fatal(err)
  671. }
  672. if len(abi.Events) != 2 {
  673. t.Error("expected 2 events")
  674. }
  675. if _, ok := abi.Events["balance"]; !ok {
  676. t.Error("expected 'balance' event to be present")
  677. }
  678. if _, ok := abi.Events["name"]; !ok {
  679. t.Error("expected 'name' event to be present")
  680. }
  681. }
  682. func TestMultiReturnWithStruct(t *testing.T) {
  683. const definition = `[
  684. { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  685. abi, err := JSON(strings.NewReader(definition))
  686. if err != nil {
  687. t.Fatal(err)
  688. }
  689. // using buff to make the code readable
  690. buff := new(bytes.Buffer)
  691. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  692. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  693. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  694. stringOut := "hello"
  695. buff.Write(common.RightPadBytes([]byte(stringOut), 32))
  696. var inter struct {
  697. Int *big.Int
  698. String string
  699. }
  700. err = abi.Unpack(&inter, "multi", buff.Bytes())
  701. if err != nil {
  702. t.Error(err)
  703. }
  704. if inter.Int == nil || inter.Int.Cmp(big.NewInt(1)) != 0 {
  705. t.Error("expected Int to be 1 got", inter.Int)
  706. }
  707. if inter.String != stringOut {
  708. t.Error("expected String to be", stringOut, "got", inter.String)
  709. }
  710. var reversed struct {
  711. String string
  712. Int *big.Int
  713. }
  714. err = abi.Unpack(&reversed, "multi", buff.Bytes())
  715. if err != nil {
  716. t.Error(err)
  717. }
  718. if reversed.Int == nil || reversed.Int.Cmp(big.NewInt(1)) != 0 {
  719. t.Error("expected Int to be 1 got", reversed.Int)
  720. }
  721. if reversed.String != stringOut {
  722. t.Error("expected String to be", stringOut, "got", reversed.String)
  723. }
  724. }
  725. func TestMultiReturnWithSlice(t *testing.T) {
  726. const definition = `[
  727. { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  728. abi, err := JSON(strings.NewReader(definition))
  729. if err != nil {
  730. t.Fatal(err)
  731. }
  732. // using buff to make the code readable
  733. buff := new(bytes.Buffer)
  734. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  735. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  736. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  737. stringOut := "hello"
  738. buff.Write(common.RightPadBytes([]byte(stringOut), 32))
  739. var inter []interface{}
  740. err = abi.Unpack(&inter, "multi", buff.Bytes())
  741. if err != nil {
  742. t.Error(err)
  743. }
  744. if len(inter) != 2 {
  745. t.Fatal("expected 2 results got", len(inter))
  746. }
  747. if num, ok := inter[0].(*big.Int); !ok || num.Cmp(big.NewInt(1)) != 0 {
  748. t.Error("expected index 0 to be 1 got", num)
  749. }
  750. if str, ok := inter[1].(string); !ok || str != stringOut {
  751. t.Error("expected index 1 to be", stringOut, "got", str)
  752. }
  753. }
  754. func TestMarshalArrays(t *testing.T) {
  755. const definition = `[
  756. { "name" : "bytes32", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  757. { "name" : "bytes10", "constant" : false, "outputs": [ { "type": "bytes10" } ] }
  758. ]`
  759. abi, err := JSON(strings.NewReader(definition))
  760. if err != nil {
  761. t.Fatal(err)
  762. }
  763. output := common.LeftPadBytes([]byte{1}, 32)
  764. var bytes10 [10]byte
  765. err = abi.Unpack(&bytes10, "bytes32", output)
  766. if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
  767. t.Error("expected error or bytes32 not be assignable to bytes10:", err)
  768. }
  769. var bytes32 [32]byte
  770. err = abi.Unpack(&bytes32, "bytes32", output)
  771. if err != nil {
  772. t.Error("didn't expect error:", err)
  773. }
  774. if !bytes.Equal(bytes32[:], output) {
  775. t.Error("expected bytes32[31] to be 1 got", bytes32[31])
  776. }
  777. type (
  778. B10 [10]byte
  779. B32 [32]byte
  780. )
  781. var b10 B10
  782. err = abi.Unpack(&b10, "bytes32", output)
  783. if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
  784. t.Error("expected error or bytes32 not be assignable to bytes10:", err)
  785. }
  786. var b32 B32
  787. err = abi.Unpack(&b32, "bytes32", output)
  788. if err != nil {
  789. t.Error("didn't expect error:", err)
  790. }
  791. if !bytes.Equal(b32[:], output) {
  792. t.Error("expected bytes32[31] to be 1 got", bytes32[31])
  793. }
  794. output[10] = 1
  795. var shortAssignLong [32]byte
  796. err = abi.Unpack(&shortAssignLong, "bytes10", output)
  797. if err != nil {
  798. t.Error("didn't expect error:", err)
  799. }
  800. if !bytes.Equal(output, shortAssignLong[:]) {
  801. t.Errorf("expected %x to be %x", shortAssignLong, output)
  802. }
  803. }
  804. func TestUnmarshal(t *testing.T) {
  805. const definition = `[
  806. { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
  807. { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
  808. { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
  809. { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  810. { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  811. { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
  812. { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
  813. { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
  814. { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  815. abi, err := JSON(strings.NewReader(definition))
  816. if err != nil {
  817. t.Fatal(err)
  818. }
  819. buff := new(bytes.Buffer)
  820. // marshal int
  821. var Int *big.Int
  822. err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  823. if err != nil {
  824. t.Error(err)
  825. }
  826. if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
  827. t.Error("expected Int to be 1 got", Int)
  828. }
  829. // marshal bool
  830. var Bool bool
  831. err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  832. if err != nil {
  833. t.Error(err)
  834. }
  835. if !Bool {
  836. t.Error("expected Bool to be true")
  837. }
  838. // marshal dynamic bytes max length 32
  839. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  840. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  841. bytesOut := common.RightPadBytes([]byte("hello"), 32)
  842. buff.Write(bytesOut)
  843. var Bytes []byte
  844. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  845. if err != nil {
  846. t.Error(err)
  847. }
  848. if !bytes.Equal(Bytes, bytesOut) {
  849. t.Errorf("expected %x got %x", bytesOut, Bytes)
  850. }
  851. // marshall dynamic bytes max length 64
  852. buff.Reset()
  853. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  854. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  855. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  856. buff.Write(bytesOut)
  857. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  858. if err != nil {
  859. t.Error(err)
  860. }
  861. if !bytes.Equal(Bytes, bytesOut) {
  862. t.Errorf("expected %x got %x", bytesOut, Bytes)
  863. }
  864. // marshall dynamic bytes max length 63
  865. buff.Reset()
  866. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  867. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  868. bytesOut = common.RightPadBytes([]byte("hello"), 63)
  869. buff.Write(bytesOut)
  870. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  871. if err != nil {
  872. t.Error(err)
  873. }
  874. if !bytes.Equal(Bytes, bytesOut) {
  875. t.Errorf("expected %x got %x", bytesOut, Bytes)
  876. }
  877. // marshal dynamic bytes output empty
  878. err = abi.Unpack(&Bytes, "bytes", nil)
  879. if err == nil {
  880. t.Error("expected error")
  881. }
  882. // marshal dynamic bytes length 5
  883. buff.Reset()
  884. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  885. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  886. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  887. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  888. if err != nil {
  889. t.Error(err)
  890. }
  891. if !bytes.Equal(Bytes, []byte("hello")) {
  892. t.Errorf("expected %x got %x", bytesOut, Bytes)
  893. }
  894. // marshal dynamic bytes length 5
  895. buff.Reset()
  896. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  897. var hash common.Hash
  898. err = abi.Unpack(&hash, "fixed", buff.Bytes())
  899. if err != nil {
  900. t.Error(err)
  901. }
  902. helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  903. if hash != helloHash {
  904. t.Errorf("Expected %x to equal %x", hash, helloHash)
  905. }
  906. // marshal error
  907. buff.Reset()
  908. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  909. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  910. if err == nil {
  911. t.Error("expected error")
  912. }
  913. err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
  914. if err == nil {
  915. t.Error("expected error")
  916. }
  917. // marshal mixed bytes
  918. buff.Reset()
  919. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  920. fixed := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")
  921. buff.Write(fixed)
  922. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  923. bytesOut = common.RightPadBytes([]byte("hello"), 32)
  924. buff.Write(bytesOut)
  925. var out []interface{}
  926. err = abi.Unpack(&out, "mixedBytes", buff.Bytes())
  927. if err != nil {
  928. t.Fatal("didn't expect error:", err)
  929. }
  930. if !bytes.Equal(bytesOut, out[0].([]byte)) {
  931. t.Errorf("expected %x, got %x", bytesOut, out[0])
  932. }
  933. if !bytes.Equal(fixed, out[1].([]byte)) {
  934. t.Errorf("expected %x, got %x", fixed, out[1])
  935. }
  936. buff.Reset()
  937. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  938. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
  939. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
  940. // marshal int array
  941. var intArray [3]*big.Int
  942. err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
  943. if err != nil {
  944. t.Error(err)
  945. }
  946. var testAgainstIntArray [3]*big.Int
  947. testAgainstIntArray[0] = big.NewInt(1)
  948. testAgainstIntArray[1] = big.NewInt(2)
  949. testAgainstIntArray[2] = big.NewInt(3)
  950. for i, Int := range intArray {
  951. if Int.Cmp(testAgainstIntArray[i]) != 0 {
  952. t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
  953. }
  954. }
  955. // marshal address slice
  956. buff.Reset()
  957. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
  958. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  959. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  960. var outAddr []common.Address
  961. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  962. if err != nil {
  963. t.Fatal("didn't expect error:", err)
  964. }
  965. if len(outAddr) != 1 {
  966. t.Fatal("expected 1 item, got", len(outAddr))
  967. }
  968. if outAddr[0] != (common.Address{1}) {
  969. t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
  970. }
  971. // marshal multiple address slice
  972. buff.Reset()
  973. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
  974. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
  975. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  976. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  977. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
  978. buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
  979. buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
  980. var outAddrStruct struct {
  981. A []common.Address
  982. B []common.Address
  983. }
  984. err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
  985. if err != nil {
  986. t.Fatal("didn't expect error:", err)
  987. }
  988. if len(outAddrStruct.A) != 1 {
  989. t.Fatal("expected 1 item, got", len(outAddrStruct.A))
  990. }
  991. if outAddrStruct.A[0] != (common.Address{1}) {
  992. t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
  993. }
  994. if len(outAddrStruct.B) != 2 {
  995. t.Fatal("expected 1 item, got", len(outAddrStruct.B))
  996. }
  997. if outAddrStruct.B[0] != (common.Address{2}) {
  998. t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
  999. }
  1000. if outAddrStruct.B[1] != (common.Address{3}) {
  1001. t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
  1002. }
  1003. // marshal invalid address slice
  1004. buff.Reset()
  1005. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
  1006. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  1007. if err == nil {
  1008. t.Fatal("expected error:", err)
  1009. }
  1010. }