args_test.go 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173
  1. package rpc
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "math/big"
  7. "testing"
  8. )
  9. func ExpectValidationError(err error) string {
  10. var str string
  11. switch err.(type) {
  12. case nil:
  13. str = "Expected error but didn't get one"
  14. case *ValidationError:
  15. break
  16. default:
  17. str = fmt.Sprintf("Expected *rpc.ValidationError but got %T with message `%s`", err, err.Error())
  18. }
  19. return str
  20. }
  21. func ExpectInvalidTypeError(err error) string {
  22. var str string
  23. switch err.(type) {
  24. case nil:
  25. str = "Expected error but didn't get one"
  26. case *InvalidTypeError:
  27. break
  28. default:
  29. str = fmt.Sprintf("Expected *rpc.InvalidTypeError but got %T with message `%s`", err, err.Error())
  30. }
  31. return str
  32. }
  33. func ExpectInsufficientParamsError(err error) string {
  34. var str string
  35. switch err.(type) {
  36. case nil:
  37. str = "Expected error but didn't get one"
  38. case *InsufficientParamsError:
  39. break
  40. default:
  41. str = fmt.Sprintf("Expected *rpc.InsufficientParamsError but got %T with message %s", err, err.Error())
  42. }
  43. return str
  44. }
  45. func ExpectDecodeParamError(err error) string {
  46. var str string
  47. switch err.(type) {
  48. case nil:
  49. str = "Expected error but didn't get one"
  50. case *DecodeParamError:
  51. break
  52. default:
  53. str = fmt.Sprintf("Expected *rpc.DecodeParamError but got %T with message `%s`", err, err.Error())
  54. }
  55. return str
  56. }
  57. func TestSha3(t *testing.T) {
  58. input := `["0x68656c6c6f20776f726c64"]`
  59. expected := "0x68656c6c6f20776f726c64"
  60. args := new(Sha3Args)
  61. json.Unmarshal([]byte(input), &args)
  62. if args.Data != expected {
  63. t.Error("got %s expected %s", input, expected)
  64. }
  65. }
  66. func TestSha3ArgsInvalid(t *testing.T) {
  67. input := `{}`
  68. args := new(Sha3Args)
  69. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  70. if len(str) > 0 {
  71. t.Error(str)
  72. }
  73. }
  74. func TestSha3ArgsEmpty(t *testing.T) {
  75. input := `[]`
  76. args := new(Sha3Args)
  77. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  78. if len(str) > 0 {
  79. t.Error(str)
  80. }
  81. }
  82. func TestSha3ArgsDataInvalid(t *testing.T) {
  83. input := `[4]`
  84. args := new(Sha3Args)
  85. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  86. if len(str) > 0 {
  87. t.Error(str)
  88. }
  89. }
  90. func TestGetBalanceArgs(t *testing.T) {
  91. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x1f"]`
  92. expected := new(GetBalanceArgs)
  93. expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
  94. expected.BlockNumber = 31
  95. args := new(GetBalanceArgs)
  96. if err := json.Unmarshal([]byte(input), &args); err != nil {
  97. t.Error(err)
  98. }
  99. if args.Address != expected.Address {
  100. t.Errorf("Address should be %v but is %v", expected.Address, args.Address)
  101. }
  102. if args.BlockNumber != expected.BlockNumber {
  103. t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber)
  104. }
  105. }
  106. func TestGetBalanceArgsLatest(t *testing.T) {
  107. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]`
  108. expected := new(GetBalanceArgs)
  109. expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
  110. expected.BlockNumber = -1
  111. args := new(GetBalanceArgs)
  112. if err := json.Unmarshal([]byte(input), &args); err != nil {
  113. t.Error(err)
  114. }
  115. if args.Address != expected.Address {
  116. t.Errorf("Address should be %v but is %v", expected.Address, args.Address)
  117. }
  118. if args.BlockNumber != expected.BlockNumber {
  119. t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber)
  120. }
  121. }
  122. func TestGetBalanceArgsEmpty(t *testing.T) {
  123. input := `[]`
  124. args := new(GetBalanceArgs)
  125. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  126. if len(str) > 0 {
  127. t.Error(str)
  128. }
  129. }
  130. func TestGetBalanceArgsInvalid(t *testing.T) {
  131. input := `6`
  132. args := new(GetBalanceArgs)
  133. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  134. if len(str) > 0 {
  135. t.Error(str)
  136. }
  137. }
  138. func TestGetBalanceArgsBlockInvalid(t *testing.T) {
  139. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", false]`
  140. args := new(GetBalanceArgs)
  141. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  142. if len(str) > 0 {
  143. t.Error(str)
  144. }
  145. }
  146. func TestGetBalanceArgsAddressInvalid(t *testing.T) {
  147. input := `[-9, "latest"]`
  148. args := new(GetBalanceArgs)
  149. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  150. if len(str) > 0 {
  151. t.Error(str)
  152. }
  153. }
  154. func TestGetBlockByHashArgs(t *testing.T) {
  155. input := `["0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", true]`
  156. expected := new(GetBlockByHashArgs)
  157. expected.BlockHash = "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331"
  158. expected.IncludeTxs = true
  159. args := new(GetBlockByHashArgs)
  160. if err := json.Unmarshal([]byte(input), &args); err != nil {
  161. t.Error(err)
  162. }
  163. if args.BlockHash != expected.BlockHash {
  164. t.Errorf("BlockHash should be %v but is %v", expected.BlockHash, args.BlockHash)
  165. }
  166. if args.IncludeTxs != expected.IncludeTxs {
  167. t.Errorf("IncludeTxs should be %v but is %v", expected.IncludeTxs, args.IncludeTxs)
  168. }
  169. }
  170. func TestGetBlockByHashArgsEmpty(t *testing.T) {
  171. input := `[]`
  172. args := new(GetBlockByHashArgs)
  173. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  174. if len(str) > 0 {
  175. t.Error(str)
  176. }
  177. }
  178. func TestGetBlockByHashArgsInvalid(t *testing.T) {
  179. input := `{}`
  180. args := new(GetBlockByHashArgs)
  181. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  182. if len(str) > 0 {
  183. t.Error(str)
  184. }
  185. }
  186. func TestGetBlockByHashArgsHashInt(t *testing.T) {
  187. input := `[8]`
  188. args := new(GetBlockByHashArgs)
  189. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  190. if len(str) > 0 {
  191. t.Error(str)
  192. }
  193. }
  194. func TestGetBlockByNumberArgsBlockNum(t *testing.T) {
  195. input := `[436, false]`
  196. expected := new(GetBlockByNumberArgs)
  197. expected.BlockNumber = 436
  198. expected.IncludeTxs = false
  199. args := new(GetBlockByNumberArgs)
  200. if err := json.Unmarshal([]byte(input), &args); err != nil {
  201. t.Error(err)
  202. }
  203. if args.BlockNumber != expected.BlockNumber {
  204. t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber)
  205. }
  206. if args.IncludeTxs != expected.IncludeTxs {
  207. t.Errorf("IncludeTxs should be %v but is %v", expected.IncludeTxs, args.IncludeTxs)
  208. }
  209. }
  210. func TestGetBlockByNumberArgsBlockHex(t *testing.T) {
  211. input := `["0x1b4", false]`
  212. expected := new(GetBlockByNumberArgs)
  213. expected.BlockNumber = 436
  214. expected.IncludeTxs = false
  215. args := new(GetBlockByNumberArgs)
  216. if err := json.Unmarshal([]byte(input), &args); err != nil {
  217. t.Error(err)
  218. }
  219. if args.BlockNumber != expected.BlockNumber {
  220. t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber)
  221. }
  222. if args.IncludeTxs != expected.IncludeTxs {
  223. t.Errorf("IncludeTxs should be %v but is %v", expected.IncludeTxs, args.IncludeTxs)
  224. }
  225. }
  226. func TestGetBlockByNumberEmpty(t *testing.T) {
  227. input := `[]`
  228. args := new(GetBlockByNumberArgs)
  229. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  230. if len(str) > 0 {
  231. t.Error(str)
  232. }
  233. }
  234. func TestGetBlockByNumberShort(t *testing.T) {
  235. input := `["0xbbb"]`
  236. args := new(GetBlockByNumberArgs)
  237. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  238. if len(str) > 0 {
  239. t.Error(str)
  240. }
  241. }
  242. func TestGetBlockByNumberBool(t *testing.T) {
  243. input := `[true, true]`
  244. args := new(GetBlockByNumberArgs)
  245. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  246. if len(str) > 0 {
  247. t.Error(str)
  248. }
  249. }
  250. func TestGetBlockByNumberBlockObject(t *testing.T) {
  251. input := `{}`
  252. args := new(GetBlockByNumberArgs)
  253. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  254. if len(str) > 0 {
  255. t.Error(str)
  256. }
  257. }
  258. func TestNewTxArgs(t *testing.T) {
  259. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  260. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  261. "gas": "0x76c0",
  262. "gasPrice": "0x9184e72a000",
  263. "value": "0x9184e72a000",
  264. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"},
  265. "0x10"]`
  266. expected := new(NewTxArgs)
  267. expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
  268. expected.To = "0xd46e8dd67c5d32be8058bb8eb970870f072445675"
  269. expected.Gas = big.NewInt(30400)
  270. expected.GasPrice = big.NewInt(10000000000000)
  271. expected.Value = big.NewInt(10000000000000)
  272. expected.Data = "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  273. expected.BlockNumber = big.NewInt(16).Int64()
  274. args := new(NewTxArgs)
  275. if err := json.Unmarshal([]byte(input), &args); err != nil {
  276. t.Error(err)
  277. }
  278. if expected.From != args.From {
  279. t.Errorf("From shoud be %#v but is %#v", expected.From, args.From)
  280. }
  281. if expected.To != args.To {
  282. t.Errorf("To shoud be %#v but is %#v", expected.To, args.To)
  283. }
  284. if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
  285. t.Errorf("Gas shoud be %#v but is %#v", expected.Gas.Bytes(), args.Gas.Bytes())
  286. }
  287. if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
  288. t.Errorf("GasPrice shoud be %#v but is %#v", expected.GasPrice, args.GasPrice)
  289. }
  290. if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 {
  291. t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value)
  292. }
  293. if expected.Data != args.Data {
  294. t.Errorf("Data shoud be %#v but is %#v", expected.Data, args.Data)
  295. }
  296. if expected.BlockNumber != args.BlockNumber {
  297. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  298. }
  299. }
  300. func TestNewTxArgsInt(t *testing.T) {
  301. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  302. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  303. "gas": 100,
  304. "gasPrice": 50,
  305. "value": 8765456789,
  306. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"},
  307. 5]`
  308. expected := new(NewTxArgs)
  309. expected.Gas = big.NewInt(100)
  310. expected.GasPrice = big.NewInt(50)
  311. expected.Value = big.NewInt(8765456789)
  312. expected.BlockNumber = int64(5)
  313. args := new(NewTxArgs)
  314. if err := json.Unmarshal([]byte(input), &args); err != nil {
  315. t.Error(err)
  316. }
  317. if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
  318. t.Errorf("Gas shoud be %v but is %v", expected.Gas, args.Gas)
  319. }
  320. if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
  321. t.Errorf("GasPrice shoud be %v but is %v", expected.GasPrice, args.GasPrice)
  322. }
  323. if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 {
  324. t.Errorf("Value shoud be %v but is %v", expected.Value, args.Value)
  325. }
  326. if expected.BlockNumber != args.BlockNumber {
  327. t.Errorf("BlockNumber shoud be %v but is %v", expected.BlockNumber, args.BlockNumber)
  328. }
  329. }
  330. func TestNewTxArgsBlockBool(t *testing.T) {
  331. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  332. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  333. "gas": "0x76c0",
  334. "gasPrice": "0x9184e72a000",
  335. "value": "0x9184e72a000",
  336. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"},
  337. false]`
  338. args := new(NewTxArgs)
  339. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  340. if len(str) > 0 {
  341. t.Error(str)
  342. }
  343. }
  344. func TestNewTxArgsGasInvalid(t *testing.T) {
  345. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  346. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  347. "gas": false,
  348. "gasPrice": "0x9184e72a000",
  349. "value": "0x9184e72a000",
  350. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  351. }]`
  352. args := new(NewTxArgs)
  353. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  354. if len(str) > 0 {
  355. t.Error(str)
  356. }
  357. }
  358. func TestNewTxArgsGaspriceInvalid(t *testing.T) {
  359. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  360. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  361. "gas": "0x76c0",
  362. "gasPrice": false,
  363. "value": "0x9184e72a000",
  364. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  365. }]`
  366. args := new(NewTxArgs)
  367. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  368. if len(str) > 0 {
  369. t.Error(str)
  370. }
  371. }
  372. func TestNewTxArgsValueInvalid(t *testing.T) {
  373. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  374. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  375. "gas": "0x76c0",
  376. "gasPrice": "0x9184e72a000",
  377. "value": false,
  378. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  379. }]`
  380. args := new(NewTxArgs)
  381. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  382. if len(str) > 0 {
  383. t.Error(str)
  384. }
  385. }
  386. func TestNewTxArgsGasMissing(t *testing.T) {
  387. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  388. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  389. "gasPrice": "0x9184e72a000",
  390. "value": "0x9184e72a000",
  391. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  392. }]`
  393. expected := new(NewTxArgs)
  394. expected.Gas = big.NewInt(0)
  395. args := new(NewTxArgs)
  396. if err := json.Unmarshal([]byte(input), &args); err != nil {
  397. t.Error(err)
  398. }
  399. if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
  400. t.Errorf("Gas shoud be %v but is %v", expected.Gas, args.Gas)
  401. }
  402. }
  403. func TestNewTxArgsBlockGaspriceMissing(t *testing.T) {
  404. input := `[{
  405. "from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  406. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  407. "gas": "0x76c0",
  408. "value": "0x9184e72a000",
  409. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  410. }]`
  411. expected := new(NewTxArgs)
  412. expected.GasPrice = big.NewInt(0)
  413. args := new(NewTxArgs)
  414. if err := json.Unmarshal([]byte(input), &args); err != nil {
  415. t.Error(err)
  416. }
  417. if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
  418. t.Errorf("GasPrice shoud be %v but is %v", expected.GasPrice, args.GasPrice)
  419. }
  420. }
  421. func TestNewTxArgsValueMissing(t *testing.T) {
  422. input := `[{
  423. "from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  424. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  425. "gas": "0x76c0",
  426. "gasPrice": "0x9184e72a000",
  427. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  428. }]`
  429. expected := new(NewTxArgs)
  430. expected.Value = big.NewInt(0)
  431. args := new(NewTxArgs)
  432. if err := json.Unmarshal([]byte(input), &args); err != nil {
  433. t.Error(err)
  434. }
  435. if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 {
  436. t.Errorf("Value shoud be %v but is %v", expected.Value, args.Value)
  437. }
  438. }
  439. func TestNewTxArgsEmpty(t *testing.T) {
  440. input := `[]`
  441. args := new(NewTxArgs)
  442. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  443. if len(str) > 0 {
  444. t.Error(str)
  445. }
  446. }
  447. func TestNewTxArgsInvalid(t *testing.T) {
  448. input := `{}`
  449. args := new(NewTxArgs)
  450. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  451. if len(str) > 0 {
  452. t.Error(str)
  453. }
  454. }
  455. func TestNewTxArgsNotStrings(t *testing.T) {
  456. input := `[{"from":6}]`
  457. args := new(NewTxArgs)
  458. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  459. if len(str) > 0 {
  460. t.Error(str)
  461. }
  462. }
  463. func TestNewTxArgsFromEmpty(t *testing.T) {
  464. input := `[{"to": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"}]`
  465. args := new(NewTxArgs)
  466. str := ExpectValidationError(json.Unmarshal([]byte(input), &args))
  467. if len(str) > 0 {
  468. t.Error(str)
  469. }
  470. }
  471. func TestCallArgs(t *testing.T) {
  472. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  473. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  474. "gas": "0x76c0",
  475. "gasPrice": "0x9184e72a000",
  476. "value": "0x9184e72a000",
  477. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"},
  478. "0x10"]`
  479. expected := new(CallArgs)
  480. expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
  481. expected.To = "0xd46e8dd67c5d32be8058bb8eb970870f072445675"
  482. expected.Gas = big.NewInt(30400)
  483. expected.GasPrice = big.NewInt(10000000000000)
  484. expected.Value = big.NewInt(10000000000000)
  485. expected.Data = "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  486. expected.BlockNumber = big.NewInt(16).Int64()
  487. args := new(CallArgs)
  488. if err := json.Unmarshal([]byte(input), &args); err != nil {
  489. t.Error(err)
  490. }
  491. if expected.From != args.From {
  492. t.Errorf("From shoud be %#v but is %#v", expected.From, args.From)
  493. }
  494. if expected.To != args.To {
  495. t.Errorf("To shoud be %#v but is %#v", expected.To, args.To)
  496. }
  497. if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
  498. t.Errorf("Gas shoud be %#v but is %#v", expected.Gas.Bytes(), args.Gas.Bytes())
  499. }
  500. if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
  501. t.Errorf("GasPrice shoud be %#v but is %#v", expected.GasPrice, args.GasPrice)
  502. }
  503. if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 {
  504. t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value)
  505. }
  506. if expected.Data != args.Data {
  507. t.Errorf("Data shoud be %#v but is %#v", expected.Data, args.Data)
  508. }
  509. if expected.BlockNumber != args.BlockNumber {
  510. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  511. }
  512. }
  513. func TestCallArgsInt(t *testing.T) {
  514. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  515. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  516. "gas": 100,
  517. "gasPrice": 50,
  518. "value": 8765456789,
  519. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"},
  520. 5]`
  521. expected := new(CallArgs)
  522. expected.Gas = big.NewInt(100)
  523. expected.GasPrice = big.NewInt(50)
  524. expected.Value = big.NewInt(8765456789)
  525. expected.BlockNumber = int64(5)
  526. args := new(CallArgs)
  527. if err := json.Unmarshal([]byte(input), &args); err != nil {
  528. t.Error(err)
  529. }
  530. if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
  531. t.Errorf("Gas shoud be %v but is %v", expected.Gas, args.Gas)
  532. }
  533. if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
  534. t.Errorf("GasPrice shoud be %v but is %v", expected.GasPrice, args.GasPrice)
  535. }
  536. if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 {
  537. t.Errorf("Value shoud be %v but is %v", expected.Value, args.Value)
  538. }
  539. if expected.BlockNumber != args.BlockNumber {
  540. t.Errorf("BlockNumber shoud be %v but is %v", expected.BlockNumber, args.BlockNumber)
  541. }
  542. }
  543. func TestCallArgsBlockBool(t *testing.T) {
  544. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  545. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  546. "gas": "0x76c0",
  547. "gasPrice": "0x9184e72a000",
  548. "value": "0x9184e72a000",
  549. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"},
  550. false]`
  551. args := new(CallArgs)
  552. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  553. if len(str) > 0 {
  554. t.Error(str)
  555. }
  556. }
  557. func TestCallArgsGasInvalid(t *testing.T) {
  558. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  559. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  560. "gas": false,
  561. "gasPrice": "0x9184e72a000",
  562. "value": "0x9184e72a000",
  563. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  564. }]`
  565. args := new(CallArgs)
  566. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  567. if len(str) > 0 {
  568. t.Error(str)
  569. }
  570. }
  571. func TestCallArgsGaspriceInvalid(t *testing.T) {
  572. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  573. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  574. "gas": "0x76c0",
  575. "gasPrice": false,
  576. "value": "0x9184e72a000",
  577. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  578. }]`
  579. args := new(CallArgs)
  580. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  581. if len(str) > 0 {
  582. t.Error(str)
  583. }
  584. }
  585. func TestCallArgsValueInvalid(t *testing.T) {
  586. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  587. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  588. "gas": "0x76c0",
  589. "gasPrice": "0x9184e72a000",
  590. "value": false,
  591. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  592. }]`
  593. args := new(CallArgs)
  594. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  595. if len(str) > 0 {
  596. t.Error(str)
  597. }
  598. }
  599. func TestCallArgsGasMissing(t *testing.T) {
  600. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  601. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  602. "gasPrice": "0x9184e72a000",
  603. "value": "0x9184e72a000",
  604. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  605. }]`
  606. args := new(CallArgs)
  607. if err := json.Unmarshal([]byte(input), &args); err != nil {
  608. t.Error(err)
  609. }
  610. expected := new(CallArgs)
  611. expected.Gas = big.NewInt(0)
  612. if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
  613. t.Errorf("Gas shoud be %v but is %v", expected.Gas, args.Gas)
  614. }
  615. }
  616. func TestCallArgsBlockGaspriceMissing(t *testing.T) {
  617. input := `[{
  618. "from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  619. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  620. "gas": "0x76c0",
  621. "value": "0x9184e72a000",
  622. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  623. }]`
  624. args := new(CallArgs)
  625. if err := json.Unmarshal([]byte(input), &args); err != nil {
  626. t.Error(err)
  627. }
  628. expected := new(CallArgs)
  629. expected.GasPrice = big.NewInt(0)
  630. if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
  631. t.Errorf("GasPrice shoud be %v but is %v", expected.GasPrice, args.GasPrice)
  632. }
  633. }
  634. func TestCallArgsValueMissing(t *testing.T) {
  635. input := `[{
  636. "from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  637. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  638. "gas": "0x76c0",
  639. "gasPrice": "0x9184e72a000",
  640. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  641. }]`
  642. args := new(CallArgs)
  643. if err := json.Unmarshal([]byte(input), &args); err != nil {
  644. t.Error(err)
  645. }
  646. expected := new(CallArgs)
  647. expected.Value = big.NewInt(int64(0))
  648. if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 {
  649. t.Errorf("GasPrice shoud be %v but is %v", expected.Value, args.Value)
  650. }
  651. }
  652. func TestCallArgsEmpty(t *testing.T) {
  653. input := `[]`
  654. args := new(CallArgs)
  655. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  656. if len(str) > 0 {
  657. t.Error(str)
  658. }
  659. }
  660. func TestCallArgsInvalid(t *testing.T) {
  661. input := `{}`
  662. args := new(CallArgs)
  663. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  664. if len(str) > 0 {
  665. t.Error(str)
  666. }
  667. }
  668. func TestCallArgsNotStrings(t *testing.T) {
  669. input := `[{"from":6}]`
  670. args := new(CallArgs)
  671. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  672. if len(str) > 0 {
  673. t.Error(str)
  674. }
  675. }
  676. func TestCallArgsFromEmpty(t *testing.T) {
  677. input := `[{"to": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"}]`
  678. args := new(CallArgs)
  679. str := ExpectValidationError(json.Unmarshal([]byte(input), &args))
  680. if len(str) > 0 {
  681. t.Error(str)
  682. }
  683. }
  684. func TestCallArgsToEmpty(t *testing.T) {
  685. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"}]`
  686. args := new(CallArgs)
  687. str := ExpectValidationError(json.Unmarshal([]byte(input), &args))
  688. if len(str) > 0 {
  689. t.Error(str)
  690. }
  691. }
  692. func TestGetStorageArgs(t *testing.T) {
  693. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]`
  694. expected := new(GetStorageArgs)
  695. expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
  696. expected.BlockNumber = -1
  697. args := new(GetStorageArgs)
  698. if err := json.Unmarshal([]byte(input), &args); err != nil {
  699. t.Error(err)
  700. }
  701. if expected.Address != args.Address {
  702. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  703. }
  704. if expected.BlockNumber != args.BlockNumber {
  705. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  706. }
  707. }
  708. func TestGetStorageInvalidArgs(t *testing.T) {
  709. input := `{}`
  710. args := new(GetStorageArgs)
  711. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  712. if len(str) > 0 {
  713. t.Error(str)
  714. }
  715. }
  716. func TestGetStorageInvalidBlockheight(t *testing.T) {
  717. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", {}]`
  718. args := new(GetStorageArgs)
  719. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  720. if len(str) > 0 {
  721. t.Error(str)
  722. }
  723. }
  724. func TestGetStorageEmptyArgs(t *testing.T) {
  725. input := `[]`
  726. args := new(GetStorageArgs)
  727. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  728. if len(str) > 0 {
  729. t.Error(str)
  730. }
  731. }
  732. func TestGetStorageAddressInt(t *testing.T) {
  733. input := `[32456785432456, "latest"]`
  734. args := new(GetStorageArgs)
  735. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  736. if len(str) > 0 {
  737. t.Error(str)
  738. }
  739. }
  740. func TestGetStorageAtArgs(t *testing.T) {
  741. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x0", "0x2"]`
  742. expected := new(GetStorageAtArgs)
  743. expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
  744. expected.Key = "0x0"
  745. expected.BlockNumber = 2
  746. args := new(GetStorageAtArgs)
  747. if err := json.Unmarshal([]byte(input), &args); err != nil {
  748. t.Error(err)
  749. }
  750. if expected.Address != args.Address {
  751. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  752. }
  753. if expected.Key != args.Key {
  754. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  755. }
  756. if expected.BlockNumber != args.BlockNumber {
  757. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  758. }
  759. }
  760. func TestGetStorageAtEmptyArgs(t *testing.T) {
  761. input := `[]`
  762. args := new(GetStorageAtArgs)
  763. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  764. if len(str) > 0 {
  765. t.Error(str)
  766. }
  767. }
  768. func TestGetStorageAtArgsInvalid(t *testing.T) {
  769. input := `{}`
  770. args := new(GetStorageAtArgs)
  771. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  772. if len(str) > 0 {
  773. t.Error(str)
  774. }
  775. }
  776. func TestGetStorageAtArgsAddressNotString(t *testing.T) {
  777. input := `[true, "0x0", "0x2"]`
  778. args := new(GetStorageAtArgs)
  779. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  780. if len(str) > 0 {
  781. t.Error(str)
  782. }
  783. }
  784. func TestGetStorageAtArgsKeyNotString(t *testing.T) {
  785. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", true, "0x2"]`
  786. args := new(GetStorageAtArgs)
  787. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  788. if len(str) > 0 {
  789. t.Error(str)
  790. }
  791. }
  792. func TestGetStorageAtArgsValueNotString(t *testing.T) {
  793. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x1", true]`
  794. args := new(GetStorageAtArgs)
  795. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  796. if len(str) > 0 {
  797. t.Error(str)
  798. }
  799. }
  800. func TestGetTxCountArgs(t *testing.T) {
  801. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "pending"]`
  802. expected := new(GetTxCountArgs)
  803. expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
  804. expected.BlockNumber = -2
  805. args := new(GetTxCountArgs)
  806. if err := json.Unmarshal([]byte(input), &args); err != nil {
  807. t.Error(err)
  808. }
  809. if expected.Address != args.Address {
  810. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  811. }
  812. if expected.BlockNumber != args.BlockNumber {
  813. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  814. }
  815. }
  816. func TestGetTxCountEmptyArgs(t *testing.T) {
  817. input := `[]`
  818. args := new(GetTxCountArgs)
  819. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  820. if len(str) > 0 {
  821. t.Error(str)
  822. }
  823. }
  824. func TestGetTxCountEmptyArgsInvalid(t *testing.T) {
  825. input := `false`
  826. args := new(GetTxCountArgs)
  827. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  828. if len(str) > 0 {
  829. t.Error(str)
  830. }
  831. }
  832. func TestGetTxCountAddressNotString(t *testing.T) {
  833. input := `[false, "pending"]`
  834. args := new(GetTxCountArgs)
  835. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  836. if len(str) > 0 {
  837. t.Error(str)
  838. }
  839. }
  840. func TestGetTxCountBlockheightInvalid(t *testing.T) {
  841. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", {}]`
  842. args := new(GetTxCountArgs)
  843. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  844. if len(str) > 0 {
  845. t.Error(str)
  846. }
  847. }
  848. func TestGetDataArgs(t *testing.T) {
  849. input := `["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8", "latest"]`
  850. expected := new(GetDataArgs)
  851. expected.Address = "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"
  852. expected.BlockNumber = -1
  853. args := new(GetDataArgs)
  854. if err := json.Unmarshal([]byte(input), &args); err != nil {
  855. t.Error(err)
  856. }
  857. if expected.Address != args.Address {
  858. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  859. }
  860. if expected.BlockNumber != args.BlockNumber {
  861. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  862. }
  863. }
  864. func TestGetDataArgsEmpty(t *testing.T) {
  865. input := `[]`
  866. args := new(GetDataArgs)
  867. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  868. if len(str) > 0 {
  869. t.Error(str)
  870. }
  871. }
  872. func TestGetDataArgsInvalid(t *testing.T) {
  873. input := `{}`
  874. args := new(GetDataArgs)
  875. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  876. if len(str) > 0 {
  877. t.Error(str)
  878. }
  879. }
  880. func TestGetDataArgsAddressNotString(t *testing.T) {
  881. input := `[12, "latest"]`
  882. args := new(GetDataArgs)
  883. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  884. if len(str) > 0 {
  885. t.Error(str)
  886. }
  887. }
  888. func TestGetDataArgsBlocknumberNotString(t *testing.T) {
  889. input := `["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8", false]`
  890. args := new(GetDataArgs)
  891. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  892. if len(str) > 0 {
  893. t.Error(str)
  894. }
  895. }
  896. func TestBlockFilterArgs(t *testing.T) {
  897. input := `[{
  898. "fromBlock": "0x1",
  899. "toBlock": "0x2",
  900. "limit": "0x3",
  901. "offset": "0x0",
  902. "address": "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8",
  903. "topics":
  904. [
  905. ["0xAA", "0xBB"],
  906. ["0xCC", "0xDD"]
  907. ]
  908. }]`
  909. expected := new(BlockFilterArgs)
  910. expected.Earliest = 1
  911. expected.Latest = 2
  912. expected.Max = 3
  913. expected.Skip = 0
  914. expected.Address = []string{"0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"}
  915. expected.Topics = [][]string{
  916. []string{"0xAA", "0xBB"},
  917. []string{"0xCC", "0xDD"},
  918. }
  919. args := new(BlockFilterArgs)
  920. if err := json.Unmarshal([]byte(input), &args); err != nil {
  921. t.Error(err)
  922. }
  923. if expected.Earliest != args.Earliest {
  924. t.Errorf("Earliest shoud be %#v but is %#v", expected.Earliest, args.Earliest)
  925. }
  926. if expected.Latest != args.Latest {
  927. t.Errorf("Latest shoud be %#v but is %#v", expected.Latest, args.Latest)
  928. }
  929. if expected.Max != args.Max {
  930. t.Errorf("Max shoud be %#v but is %#v", expected.Max, args.Max)
  931. }
  932. if expected.Skip != args.Skip {
  933. t.Errorf("Skip shoud be %#v but is %#v", expected.Skip, args.Skip)
  934. }
  935. if expected.Address[0] != args.Address[0] {
  936. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  937. }
  938. if expected.Topics[0][0] != args.Topics[0][0] {
  939. t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics)
  940. }
  941. if expected.Topics[0][1] != args.Topics[0][1] {
  942. t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics)
  943. }
  944. if expected.Topics[1][0] != args.Topics[1][0] {
  945. t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics)
  946. }
  947. if expected.Topics[1][1] != args.Topics[1][1] {
  948. t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics)
  949. }
  950. }
  951. func TestBlockFilterArgsDefaults(t *testing.T) {
  952. input := `[{
  953. "address": ["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"],
  954. "topics": ["0xAA","0xBB"]
  955. }]`
  956. expected := new(BlockFilterArgs)
  957. expected.Earliest = -1
  958. expected.Latest = -1
  959. expected.Max = 100
  960. expected.Skip = 0
  961. expected.Address = []string{"0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"}
  962. expected.Topics = [][]string{[]string{"0xAA"}, []string{"0xBB"}}
  963. args := new(BlockFilterArgs)
  964. if err := json.Unmarshal([]byte(input), &args); err != nil {
  965. t.Error(err)
  966. }
  967. if expected.Earliest != args.Earliest {
  968. t.Errorf("Earliest shoud be %#v but is %#v", expected.Earliest, args.Earliest)
  969. }
  970. if expected.Latest != args.Latest {
  971. t.Errorf("Latest shoud be %#v but is %#v", expected.Latest, args.Latest)
  972. }
  973. if expected.Max != args.Max {
  974. t.Errorf("Max shoud be %#v but is %#v", expected.Max, args.Max)
  975. }
  976. if expected.Skip != args.Skip {
  977. t.Errorf("Skip shoud be %#v but is %#v", expected.Skip, args.Skip)
  978. }
  979. if expected.Address[0] != args.Address[0] {
  980. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  981. }
  982. if expected.Topics[0][0] != args.Topics[0][0] {
  983. t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics)
  984. }
  985. if expected.Topics[1][0] != args.Topics[1][0] {
  986. t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics)
  987. }
  988. }
  989. func TestBlockFilterArgsWords(t *testing.T) {
  990. input := `[{
  991. "fromBlock": "latest",
  992. "toBlock": "pending"
  993. }]`
  994. expected := new(BlockFilterArgs)
  995. expected.Earliest = -1
  996. expected.Latest = -2
  997. args := new(BlockFilterArgs)
  998. if err := json.Unmarshal([]byte(input), &args); err != nil {
  999. t.Error(err)
  1000. }
  1001. if expected.Earliest != args.Earliest {
  1002. t.Errorf("Earliest shoud be %#v but is %#v", expected.Earliest, args.Earliest)
  1003. }
  1004. if expected.Latest != args.Latest {
  1005. t.Errorf("Latest shoud be %#v but is %#v", expected.Latest, args.Latest)
  1006. }
  1007. }
  1008. func TestBlockFilterArgsInvalid(t *testing.T) {
  1009. input := `{}`
  1010. args := new(BlockFilterArgs)
  1011. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  1012. if len(str) > 0 {
  1013. t.Error(str)
  1014. }
  1015. }
  1016. func TestBlockFilterArgsFromBool(t *testing.T) {
  1017. input := `[{
  1018. "fromBlock": true,
  1019. "toBlock": "pending"
  1020. }]`
  1021. args := new(BlockFilterArgs)
  1022. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1023. if len(str) > 0 {
  1024. t.Error(str)
  1025. }
  1026. }
  1027. func TestBlockFilterArgsToBool(t *testing.T) {
  1028. input := `[{
  1029. "fromBlock": "pending",
  1030. "toBlock": true
  1031. }]`
  1032. args := new(BlockFilterArgs)
  1033. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1034. if len(str) > 0 {
  1035. t.Error(str)
  1036. }
  1037. }
  1038. func TestBlockFilterArgsEmptyArgs(t *testing.T) {
  1039. input := `[]`
  1040. args := new(BlockFilterArgs)
  1041. err := json.Unmarshal([]byte(input), &args)
  1042. if err == nil {
  1043. t.Error("Expected error but didn't get one")
  1044. }
  1045. }
  1046. func TestBlockFilterArgsLimitInvalid(t *testing.T) {
  1047. input := `[{"limit": false}]`
  1048. args := new(BlockFilterArgs)
  1049. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1050. if len(str) > 0 {
  1051. t.Error(str)
  1052. }
  1053. }
  1054. func TestBlockFilterArgsOffsetInvalid(t *testing.T) {
  1055. input := `[{"offset": true}]`
  1056. args := new(BlockFilterArgs)
  1057. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1058. if len(str) > 0 {
  1059. t.Error(str)
  1060. }
  1061. }
  1062. func TestBlockFilterArgsAddressInt(t *testing.T) {
  1063. input := `[{
  1064. "address": 1,
  1065. "topics": "0x12341234"}]`
  1066. args := new(BlockFilterArgs)
  1067. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1068. if len(str) > 0 {
  1069. t.Error(str)
  1070. }
  1071. }
  1072. func TestBlockFilterArgsAddressSliceInt(t *testing.T) {
  1073. input := `[{
  1074. "address": [1],
  1075. "topics": "0x12341234"}]`
  1076. args := new(BlockFilterArgs)
  1077. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1078. if len(str) > 0 {
  1079. t.Error(str)
  1080. }
  1081. }
  1082. func TestBlockFilterArgsTopicInt(t *testing.T) {
  1083. input := `[{
  1084. "address": ["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"],
  1085. "topics": 1}]`
  1086. args := new(BlockFilterArgs)
  1087. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1088. if len(str) > 0 {
  1089. t.Error(str)
  1090. }
  1091. }
  1092. func TestBlockFilterArgsTopicSliceInt(t *testing.T) {
  1093. input := `[{
  1094. "address": "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8",
  1095. "topics": [1]}]`
  1096. args := new(BlockFilterArgs)
  1097. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1098. if len(str) > 0 {
  1099. t.Error(str)
  1100. }
  1101. }
  1102. func TestBlockFilterArgsTopicSliceInt2(t *testing.T) {
  1103. input := `[{
  1104. "address": "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8",
  1105. "topics": ["0xAA", [1]]}]`
  1106. args := new(BlockFilterArgs)
  1107. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1108. if len(str) > 0 {
  1109. t.Error(str)
  1110. }
  1111. }
  1112. func TestBlockFilterArgsTopicComplex(t *testing.T) {
  1113. input := `[{
  1114. "address": "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8",
  1115. "topics": ["0xAA", ["0xBB", "0xCC"]]
  1116. }]`
  1117. args := new(BlockFilterArgs)
  1118. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1119. t.Error(err)
  1120. fmt.Printf("%v\n", args)
  1121. return
  1122. }
  1123. if args.Topics[0][0] != "0xAA" {
  1124. t.Errorf("Topic should be %s but is %s", "0xAA", args.Topics[0][0])
  1125. }
  1126. if args.Topics[1][0] != "0xBB" {
  1127. t.Errorf("Topic should be %s but is %s", "0xBB", args.Topics[0][0])
  1128. }
  1129. if args.Topics[1][1] != "0xCC" {
  1130. t.Errorf("Topic should be %s but is %s", "0xCC", args.Topics[0][0])
  1131. }
  1132. }
  1133. func TestDbArgs(t *testing.T) {
  1134. input := `["testDB","myKey","0xbeef"]`
  1135. expected := new(DbArgs)
  1136. expected.Database = "testDB"
  1137. expected.Key = "myKey"
  1138. expected.Value = []byte("0xbeef")
  1139. args := new(DbArgs)
  1140. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1141. t.Error(err)
  1142. }
  1143. if err := args.requirements(); err != nil {
  1144. t.Error(err)
  1145. }
  1146. if expected.Database != args.Database {
  1147. t.Errorf("Database shoud be %#v but is %#v", expected.Database, args.Database)
  1148. }
  1149. if expected.Key != args.Key {
  1150. t.Errorf("Key shoud be %#v but is %#v", expected.Key, args.Key)
  1151. }
  1152. if bytes.Compare(expected.Value, args.Value) != 0 {
  1153. t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value)
  1154. }
  1155. }
  1156. func TestDbArgsInvalid(t *testing.T) {
  1157. input := `{}`
  1158. args := new(DbArgs)
  1159. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  1160. if len(str) > 0 {
  1161. t.Error(str)
  1162. }
  1163. }
  1164. func TestDbArgsEmpty(t *testing.T) {
  1165. input := `[]`
  1166. args := new(DbArgs)
  1167. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  1168. if len(str) > 0 {
  1169. t.Error(str)
  1170. }
  1171. }
  1172. func TestDbArgsDatabaseType(t *testing.T) {
  1173. input := `[true, "keyval", "valval"]`
  1174. args := new(DbArgs)
  1175. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1176. if len(str) > 0 {
  1177. t.Error(str)
  1178. }
  1179. }
  1180. func TestDbArgsKeyType(t *testing.T) {
  1181. input := `["dbval", 3, "valval"]`
  1182. args := new(DbArgs)
  1183. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1184. if len(str) > 0 {
  1185. t.Error(str)
  1186. }
  1187. }
  1188. func TestDbArgsValType(t *testing.T) {
  1189. input := `["dbval", "keyval", {}]`
  1190. args := new(DbArgs)
  1191. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1192. if len(str) > 0 {
  1193. t.Error(str)
  1194. }
  1195. }
  1196. func TestDbArgsDatabaseEmpty(t *testing.T) {
  1197. input := `["", "keyval", "valval"]`
  1198. args := new(DbArgs)
  1199. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1200. t.Error(err.Error())
  1201. }
  1202. str := ExpectValidationError(args.requirements())
  1203. if len(str) > 0 {
  1204. t.Error(str)
  1205. }
  1206. }
  1207. func TestDbArgsKeyEmpty(t *testing.T) {
  1208. input := `["dbval", "", "valval"]`
  1209. args := new(DbArgs)
  1210. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1211. t.Error(err.Error())
  1212. }
  1213. str := ExpectValidationError(args.requirements())
  1214. if len(str) > 0 {
  1215. t.Error(str)
  1216. }
  1217. }
  1218. func TestDbHexArgs(t *testing.T) {
  1219. input := `["testDB","myKey","0xbeef"]`
  1220. expected := new(DbHexArgs)
  1221. expected.Database = "testDB"
  1222. expected.Key = "myKey"
  1223. expected.Value = []byte{0xbe, 0xef}
  1224. args := new(DbHexArgs)
  1225. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1226. t.Error(err)
  1227. }
  1228. if err := args.requirements(); err != nil {
  1229. t.Error(err)
  1230. }
  1231. if expected.Database != args.Database {
  1232. t.Errorf("Database shoud be %#v but is %#v", expected.Database, args.Database)
  1233. }
  1234. if expected.Key != args.Key {
  1235. t.Errorf("Key shoud be %#v but is %#v", expected.Key, args.Key)
  1236. }
  1237. if bytes.Compare(expected.Value, args.Value) != 0 {
  1238. t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value)
  1239. }
  1240. }
  1241. func TestDbHexArgsInvalid(t *testing.T) {
  1242. input := `{}`
  1243. args := new(DbHexArgs)
  1244. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  1245. if len(str) > 0 {
  1246. t.Error(str)
  1247. }
  1248. }
  1249. func TestDbHexArgsEmpty(t *testing.T) {
  1250. input := `[]`
  1251. args := new(DbHexArgs)
  1252. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  1253. if len(str) > 0 {
  1254. t.Error(str)
  1255. }
  1256. }
  1257. func TestDbHexArgsDatabaseType(t *testing.T) {
  1258. input := `[true, "keyval", "valval"]`
  1259. args := new(DbHexArgs)
  1260. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1261. if len(str) > 0 {
  1262. t.Error(str)
  1263. }
  1264. }
  1265. func TestDbHexArgsKeyType(t *testing.T) {
  1266. input := `["dbval", 3, "valval"]`
  1267. args := new(DbHexArgs)
  1268. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1269. if len(str) > 0 {
  1270. t.Error(str)
  1271. }
  1272. }
  1273. func TestDbHexArgsValType(t *testing.T) {
  1274. input := `["dbval", "keyval", {}]`
  1275. args := new(DbHexArgs)
  1276. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1277. if len(str) > 0 {
  1278. t.Error(str)
  1279. }
  1280. }
  1281. func TestDbHexArgsDatabaseEmpty(t *testing.T) {
  1282. input := `["", "keyval", "valval"]`
  1283. args := new(DbHexArgs)
  1284. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1285. t.Error(err.Error())
  1286. }
  1287. str := ExpectValidationError(args.requirements())
  1288. if len(str) > 0 {
  1289. t.Error(str)
  1290. }
  1291. }
  1292. func TestDbHexArgsKeyEmpty(t *testing.T) {
  1293. input := `["dbval", "", "valval"]`
  1294. args := new(DbHexArgs)
  1295. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1296. t.Error(err.Error())
  1297. }
  1298. str := ExpectValidationError(args.requirements())
  1299. if len(str) > 0 {
  1300. t.Error(str)
  1301. }
  1302. }
  1303. func TestWhisperMessageArgs(t *testing.T) {
  1304. input := `[{"from":"0xc931d93e97ab07fe42d923478ba2465f2",
  1305. "topics": ["0x68656c6c6f20776f726c64"],
  1306. "payload":"0x68656c6c6f20776f726c64",
  1307. "ttl": "0x64",
  1308. "priority": "0x64"}]`
  1309. expected := new(WhisperMessageArgs)
  1310. expected.From = "0xc931d93e97ab07fe42d923478ba2465f2"
  1311. expected.To = ""
  1312. expected.Payload = "0x68656c6c6f20776f726c64"
  1313. expected.Priority = 100
  1314. expected.Ttl = 100
  1315. // expected.Topics = []string{"0x68656c6c6f20776f726c64"}
  1316. args := new(WhisperMessageArgs)
  1317. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1318. t.Error(err)
  1319. }
  1320. if expected.From != args.From {
  1321. t.Errorf("From shoud be %#v but is %#v", expected.From, args.From)
  1322. }
  1323. if expected.To != args.To {
  1324. t.Errorf("To shoud be %#v but is %#v", expected.To, args.To)
  1325. }
  1326. if expected.Payload != args.Payload {
  1327. t.Errorf("Value shoud be %#v but is %#v", expected.Payload, args.Payload)
  1328. }
  1329. if expected.Ttl != args.Ttl {
  1330. t.Errorf("Ttl shoud be %#v but is %#v", expected.Ttl, args.Ttl)
  1331. }
  1332. if expected.Priority != args.Priority {
  1333. t.Errorf("Priority shoud be %#v but is %#v", expected.Priority, args.Priority)
  1334. }
  1335. // if expected.Topics != args.Topics {
  1336. // t.Errorf("Topic shoud be %#v but is %#v", expected.Topic, args.Topic)
  1337. // }
  1338. }
  1339. func TestWhisperMessageArgsInt(t *testing.T) {
  1340. input := `[{"from":"0xc931d93e97ab07fe42d923478ba2465f2",
  1341. "topics": ["0x68656c6c6f20776f726c64"],
  1342. "payload":"0x68656c6c6f20776f726c64",
  1343. "ttl": 12,
  1344. "priority": 16}]`
  1345. expected := new(WhisperMessageArgs)
  1346. expected.From = "0xc931d93e97ab07fe42d923478ba2465f2"
  1347. expected.To = ""
  1348. expected.Payload = "0x68656c6c6f20776f726c64"
  1349. expected.Priority = 16
  1350. expected.Ttl = 12
  1351. // expected.Topics = []string{"0x68656c6c6f20776f726c64"}
  1352. args := new(WhisperMessageArgs)
  1353. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1354. t.Error(err)
  1355. }
  1356. if expected.From != args.From {
  1357. t.Errorf("From shoud be %#v but is %#v", expected.From, args.From)
  1358. }
  1359. if expected.To != args.To {
  1360. t.Errorf("To shoud be %#v but is %#v", expected.To, args.To)
  1361. }
  1362. if expected.Payload != args.Payload {
  1363. t.Errorf("Value shoud be %#v but is %#v", expected.Payload, args.Payload)
  1364. }
  1365. if expected.Ttl != args.Ttl {
  1366. t.Errorf("Ttl shoud be %v but is %v", expected.Ttl, args.Ttl)
  1367. }
  1368. if expected.Priority != args.Priority {
  1369. t.Errorf("Priority shoud be %v but is %v", expected.Priority, args.Priority)
  1370. }
  1371. // if expected.Topics != args.Topics {
  1372. // t.Errorf("Topic shoud be %#v but is %#v", expected.Topic, args.Topic)
  1373. // }
  1374. }
  1375. func TestWhisperMessageArgsInvalid(t *testing.T) {
  1376. input := `{}`
  1377. args := new(WhisperMessageArgs)
  1378. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), args))
  1379. if len(str) > 0 {
  1380. t.Error(str)
  1381. }
  1382. }
  1383. func TestWhisperMessageArgsEmpty(t *testing.T) {
  1384. input := `[]`
  1385. args := new(WhisperMessageArgs)
  1386. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args))
  1387. if len(str) > 0 {
  1388. t.Error(str)
  1389. }
  1390. }
  1391. func TestWhisperMessageArgsTtlBool(t *testing.T) {
  1392. input := `[{"from":"0xc931d93e97ab07fe42d923478ba2465f2",
  1393. "topics": ["0x68656c6c6f20776f726c64"],
  1394. "payload":"0x68656c6c6f20776f726c64",
  1395. "ttl": true,
  1396. "priority": "0x64"}]`
  1397. args := new(WhisperMessageArgs)
  1398. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args))
  1399. if len(str) > 0 {
  1400. t.Error(str)
  1401. }
  1402. }
  1403. func TestWhisperMessageArgsPriorityBool(t *testing.T) {
  1404. input := `[{"from":"0xc931d93e97ab07fe42d923478ba2465f2",
  1405. "topics": ["0x68656c6c6f20776f726c64"],
  1406. "payload":"0x68656c6c6f20776f726c64",
  1407. "ttl": "0x12",
  1408. "priority": true}]`
  1409. args := new(WhisperMessageArgs)
  1410. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args))
  1411. if len(str) > 0 {
  1412. t.Error(str)
  1413. }
  1414. }
  1415. func TestFilterIdArgs(t *testing.T) {
  1416. input := `["0x7"]`
  1417. expected := new(FilterIdArgs)
  1418. expected.Id = 7
  1419. args := new(FilterIdArgs)
  1420. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1421. t.Error(err)
  1422. }
  1423. if expected.Id != args.Id {
  1424. t.Errorf("Id shoud be %#v but is %#v", expected.Id, args.Id)
  1425. }
  1426. }
  1427. func TestFilterIdArgsInvalid(t *testing.T) {
  1428. input := `{}`
  1429. args := new(FilterIdArgs)
  1430. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  1431. if len(str) > 0 {
  1432. t.Errorf(str)
  1433. }
  1434. }
  1435. func TestFilterIdArgsEmpty(t *testing.T) {
  1436. input := `[]`
  1437. args := new(FilterIdArgs)
  1438. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  1439. if len(str) > 0 {
  1440. t.Errorf(str)
  1441. }
  1442. }
  1443. func TestFilterIdArgsBool(t *testing.T) {
  1444. input := `[true]`
  1445. args := new(FilterIdArgs)
  1446. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1447. if len(str) > 0 {
  1448. t.Errorf(str)
  1449. }
  1450. }
  1451. func TestWhisperFilterArgs(t *testing.T) {
  1452. input := `[{"topics": ["0x68656c6c6f20776f726c64"], "to": "0x34ag445g3455b34"}]`
  1453. expected := new(WhisperFilterArgs)
  1454. expected.To = "0x34ag445g3455b34"
  1455. expected.Topics = []string{"0x68656c6c6f20776f726c64"}
  1456. args := new(WhisperFilterArgs)
  1457. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1458. t.Error(err)
  1459. }
  1460. if expected.To != args.To {
  1461. t.Errorf("To shoud be %#v but is %#v", expected.To, args.To)
  1462. }
  1463. // if expected.Topics != args.Topics {
  1464. // t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics)
  1465. // }
  1466. }
  1467. func TestWhisperFilterArgsInvalid(t *testing.T) {
  1468. input := `{}`
  1469. args := new(WhisperFilterArgs)
  1470. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), args))
  1471. if len(str) > 0 {
  1472. t.Error(str)
  1473. }
  1474. }
  1475. func TestWhisperFilterArgsEmpty(t *testing.T) {
  1476. input := `[]`
  1477. args := new(WhisperFilterArgs)
  1478. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args))
  1479. if len(str) > 0 {
  1480. t.Error(str)
  1481. }
  1482. }
  1483. func TestWhisperFilterArgsToBool(t *testing.T) {
  1484. input := `[{"topics": ["0x68656c6c6f20776f726c64"], "to": false}]`
  1485. args := new(WhisperFilterArgs)
  1486. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args))
  1487. if len(str) > 0 {
  1488. t.Error(str)
  1489. }
  1490. }
  1491. func TestWhisperFilterArgsTopicInt(t *testing.T) {
  1492. input := `[{"topics": [6], "to": "0x34ag445g3455b34"}]`
  1493. args := new(WhisperFilterArgs)
  1494. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args))
  1495. if len(str) > 0 {
  1496. t.Error(str)
  1497. }
  1498. }
  1499. func TestCompileArgs(t *testing.T) {
  1500. input := `["contract test { function multiply(uint a) returns(uint d) { return a * 7; } }"]`
  1501. expected := new(CompileArgs)
  1502. expected.Source = `contract test { function multiply(uint a) returns(uint d) { return a * 7; } }`
  1503. args := new(CompileArgs)
  1504. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1505. t.Error(err)
  1506. }
  1507. if expected.Source != args.Source {
  1508. t.Errorf("Source shoud be %#v but is %#v", expected.Source, args.Source)
  1509. }
  1510. }
  1511. func TestCompileArgsInvalid(t *testing.T) {
  1512. input := `{}`
  1513. args := new(CompileArgs)
  1514. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), args))
  1515. if len(str) > 0 {
  1516. t.Error(str)
  1517. }
  1518. }
  1519. func TestCompileArgsEmpty(t *testing.T) {
  1520. input := `[]`
  1521. args := new(CompileArgs)
  1522. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args))
  1523. if len(str) > 0 {
  1524. t.Error(str)
  1525. }
  1526. }
  1527. func TestCompileArgsBool(t *testing.T) {
  1528. input := `[false]`
  1529. args := new(CompileArgs)
  1530. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args))
  1531. if len(str) > 0 {
  1532. t.Error(str)
  1533. }
  1534. }
  1535. func TestFilterStringArgs(t *testing.T) {
  1536. input := `["pending"]`
  1537. expected := new(FilterStringArgs)
  1538. expected.Word = "pending"
  1539. args := new(FilterStringArgs)
  1540. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1541. t.Error(err)
  1542. }
  1543. if expected.Word != args.Word {
  1544. t.Errorf("Word shoud be %#v but is %#v", expected.Word, args.Word)
  1545. }
  1546. }
  1547. func TestFilterStringEmptyArgs(t *testing.T) {
  1548. input := `[]`
  1549. args := new(FilterStringArgs)
  1550. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  1551. if len(str) > 0 {
  1552. t.Errorf(str)
  1553. }
  1554. }
  1555. func TestFilterStringInvalidArgs(t *testing.T) {
  1556. input := `{}`
  1557. args := new(FilterStringArgs)
  1558. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  1559. if len(str) > 0 {
  1560. t.Errorf(str)
  1561. }
  1562. }
  1563. func TestFilterStringWordInt(t *testing.T) {
  1564. input := `[7]`
  1565. args := new(FilterStringArgs)
  1566. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1567. if len(str) > 0 {
  1568. t.Errorf(str)
  1569. }
  1570. }
  1571. func TestFilterStringWordWrong(t *testing.T) {
  1572. input := `["foo"]`
  1573. args := new(FilterStringArgs)
  1574. str := ExpectValidationError(json.Unmarshal([]byte(input), &args))
  1575. if len(str) > 0 {
  1576. t.Errorf(str)
  1577. }
  1578. }
  1579. func TestWhisperIdentityArgs(t *testing.T) {
  1580. input := `["0xc931d93e97ab07fe42d923478ba2465f283"]`
  1581. expected := new(WhisperIdentityArgs)
  1582. expected.Identity = "0xc931d93e97ab07fe42d923478ba2465f283"
  1583. args := new(WhisperIdentityArgs)
  1584. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1585. t.Error(err)
  1586. }
  1587. if expected.Identity != args.Identity {
  1588. t.Errorf("Identity shoud be %#v but is %#v", expected.Identity, args.Identity)
  1589. }
  1590. }
  1591. func TestWhisperIdentityArgsInvalid(t *testing.T) {
  1592. input := `{}`
  1593. args := new(WhisperIdentityArgs)
  1594. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  1595. if len(str) > 0 {
  1596. t.Errorf(str)
  1597. }
  1598. }
  1599. func TestWhisperIdentityArgsEmpty(t *testing.T) {
  1600. input := `[]`
  1601. args := new(WhisperIdentityArgs)
  1602. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  1603. if len(str) > 0 {
  1604. t.Errorf(str)
  1605. }
  1606. }
  1607. func TestWhisperIdentityArgsInt(t *testing.T) {
  1608. input := `[4]`
  1609. args := new(WhisperIdentityArgs)
  1610. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1611. if len(str) > 0 {
  1612. t.Errorf(str)
  1613. }
  1614. }
  1615. func TestBlockNumIndexArgs(t *testing.T) {
  1616. input := `["0x29a", "0x0"]`
  1617. expected := new(BlockNumIndexArgs)
  1618. expected.BlockNumber = 666
  1619. expected.Index = 0
  1620. args := new(BlockNumIndexArgs)
  1621. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1622. t.Error(err)
  1623. }
  1624. if expected.BlockNumber != args.BlockNumber {
  1625. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  1626. }
  1627. if expected.Index != args.Index {
  1628. t.Errorf("Index shoud be %#v but is %#v", expected.Index, args.Index)
  1629. }
  1630. }
  1631. func TestBlockNumIndexArgsEmpty(t *testing.T) {
  1632. input := `[]`
  1633. args := new(BlockNumIndexArgs)
  1634. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  1635. if len(str) > 0 {
  1636. t.Error(str)
  1637. }
  1638. }
  1639. func TestBlockNumIndexArgsInvalid(t *testing.T) {
  1640. input := `"foo"`
  1641. args := new(BlockNumIndexArgs)
  1642. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  1643. if len(str) > 0 {
  1644. t.Error(str)
  1645. }
  1646. }
  1647. func TestBlockNumIndexArgsBlocknumInvalid(t *testing.T) {
  1648. input := `[{}, "0x1"]`
  1649. args := new(BlockNumIndexArgs)
  1650. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1651. if len(str) > 0 {
  1652. t.Error(str)
  1653. }
  1654. }
  1655. func TestBlockNumIndexArgsIndexInvalid(t *testing.T) {
  1656. input := `["0x29a", 1]`
  1657. args := new(BlockNumIndexArgs)
  1658. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1659. if len(str) > 0 {
  1660. t.Error(str)
  1661. }
  1662. }
  1663. func TestHashIndexArgs(t *testing.T) {
  1664. input := `["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", "0x1"]`
  1665. expected := new(HashIndexArgs)
  1666. expected.Hash = "0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"
  1667. expected.Index = 1
  1668. args := new(HashIndexArgs)
  1669. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1670. t.Error(err)
  1671. }
  1672. if expected.Hash != args.Hash {
  1673. t.Errorf("Hash shoud be %#v but is %#v", expected.Hash, args.Hash)
  1674. }
  1675. if expected.Index != args.Index {
  1676. t.Errorf("Index shoud be %#v but is %#v", expected.Index, args.Index)
  1677. }
  1678. }
  1679. func TestHashIndexArgsEmpty(t *testing.T) {
  1680. input := `[]`
  1681. args := new(HashIndexArgs)
  1682. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
  1683. if len(str) > 0 {
  1684. t.Error(str)
  1685. }
  1686. }
  1687. func TestHashIndexArgsInvalid(t *testing.T) {
  1688. input := `{}`
  1689. args := new(HashIndexArgs)
  1690. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
  1691. if len(str) > 0 {
  1692. t.Error(str)
  1693. }
  1694. }
  1695. func TestHashIndexArgsInvalidHash(t *testing.T) {
  1696. input := `[7, "0x1"]`
  1697. args := new(HashIndexArgs)
  1698. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1699. if len(str) > 0 {
  1700. t.Error(str)
  1701. }
  1702. }
  1703. func TestHashIndexArgsInvalidIndex(t *testing.T) {
  1704. input := `["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", false]`
  1705. args := new(HashIndexArgs)
  1706. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
  1707. if len(str) > 0 {
  1708. t.Error(str)
  1709. }
  1710. }
  1711. func TestSubmitWorkArgs(t *testing.T) {
  1712. input := `["0x0000000000000001", "0x1234567890abcdef1234567890abcdef", "0xD1GE5700000000000000000000000000"]`
  1713. expected := new(SubmitWorkArgs)
  1714. expected.Nonce = 1
  1715. expected.Header = "0x1234567890abcdef1234567890abcdef"
  1716. expected.Digest = "0xD1GE5700000000000000000000000000"
  1717. args := new(SubmitWorkArgs)
  1718. if err := json.Unmarshal([]byte(input), &args); err != nil {
  1719. t.Error(err)
  1720. }
  1721. if expected.Nonce != args.Nonce {
  1722. t.Errorf("Nonce shoud be %d but is %d", expected.Nonce, args.Nonce)
  1723. }
  1724. if expected.Header != args.Header {
  1725. t.Errorf("Header shoud be %#v but is %#v", expected.Header, args.Header)
  1726. }
  1727. if expected.Digest != args.Digest {
  1728. t.Errorf("Digest shoud be %#v but is %#v", expected.Digest, args.Digest)
  1729. }
  1730. }
  1731. func TestSubmitWorkArgsInvalid(t *testing.T) {
  1732. input := `{}`
  1733. args := new(SubmitWorkArgs)
  1734. str := ExpectDecodeParamError(json.Unmarshal([]byte(input), args))
  1735. if len(str) > 0 {
  1736. t.Error(str)
  1737. }
  1738. }
  1739. func TestSubmitWorkArgsEmpty(t *testing.T) {
  1740. input := `[]`
  1741. args := new(SubmitWorkArgs)
  1742. str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args))
  1743. if len(str) > 0 {
  1744. t.Error(str)
  1745. }
  1746. }
  1747. func TestSubmitWorkArgsNonceInt(t *testing.T) {
  1748. input := `[1, "0x1234567890abcdef1234567890abcdef", "0xD1GE5700000000000000000000000000"]`
  1749. args := new(SubmitWorkArgs)
  1750. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args))
  1751. if len(str) > 0 {
  1752. t.Error(str)
  1753. }
  1754. }
  1755. func TestSubmitWorkArgsHeaderInt(t *testing.T) {
  1756. input := `["0x0000000000000001", 1, "0xD1GE5700000000000000000000000000"]`
  1757. args := new(SubmitWorkArgs)
  1758. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args))
  1759. if len(str) > 0 {
  1760. t.Error(str)
  1761. }
  1762. }
  1763. func TestSubmitWorkArgsDigestInt(t *testing.T) {
  1764. input := `["0x0000000000000001", "0x1234567890abcdef1234567890abcdef", 1]`
  1765. args := new(SubmitWorkArgs)
  1766. str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args))
  1767. if len(str) > 0 {
  1768. t.Error(str)
  1769. }
  1770. }
  1771. func TestBlockHeightFromJsonInvalid(t *testing.T) {
  1772. var num int64
  1773. var msg json.RawMessage = []byte(`}{`)
  1774. str := ExpectDecodeParamError(blockHeightFromJson(msg, &num))
  1775. if len(str) > 0 {
  1776. t.Error(str)
  1777. }
  1778. }