PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/accounts/abi/abi_test.go

https://gitlab.com/akomba/ether-bot-wallet
Go | 395 lines | 317 code | 60 blank | 18 comment | 104 complexity | 631213ba0a2178cc5ee4a1435614c279 MD5 | raw file
  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. { "name" : "balance", "const" : true },
  31. { "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
  32. ]`
  33. const jsondata2 = `
  34. [
  35. { "name" : "balance", "const" : true },
  36. { "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
  37. { "name" : "test", "const" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
  38. { "name" : "string", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
  39. { "name" : "bool", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
  40. { "name" : "address", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
  41. { "name" : "string32", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string32" } ] },
  42. { "name" : "uint64[2]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
  43. { "name" : "uint64[]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
  44. { "name" : "foo", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
  45. { "name" : "bar", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
  46. { "name" : "slice", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
  47. { "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, Type{},
  87. },
  88. "send": Method{
  89. "send", false, []Argument{
  90. Argument{"amount", Uint256},
  91. }, Type{},
  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}, Argument{"baz", String}}, Type{}}
  207. exp := "foo(string32,string)"
  208. if m.String() != exp {
  209. t.Error("signature mismatch", exp, "!=", m.String())
  210. }
  211. idexp := crypto.Sha3([]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}}, Type{}}
  217. exp = "foo(uint256)"
  218. if m.String() != exp {
  219. t.Error("signature mismatch", exp, "!=", m.String())
  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.Sha3([]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.Sha3([]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.Sha3([]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.Sha3([]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. { "name" : "balance", "const" : true, "inputs" : [ { "name" : "address", "type" : "bytes20" } ] },
  314. { "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. }