You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
9.0 KiB

  1. package api
  2. import (
  3. "encoding/base64"
  4. "math/big"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. ethCommon "github.com/ethereum/go-ethereum/common"
  9. "github.com/hermeznetwork/hermez-node/common"
  10. "github.com/iden3/go-iden3-crypto/babyjub"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. type queryParser struct {
  14. m map[string]string
  15. }
  16. func (qp *queryParser) Query(query string) string {
  17. if val, ok := qp.m[query]; ok {
  18. return val
  19. }
  20. return ""
  21. }
  22. func (qp *queryParser) Param(param string) string {
  23. if val, ok := qp.m[param]; ok {
  24. return val
  25. }
  26. return ""
  27. }
  28. func TestParseQueryUint(t *testing.T) {
  29. name := "foo"
  30. c := &queryParser{}
  31. c.m = make(map[string]string)
  32. var min uint = 1
  33. var max uint = 10
  34. var dflt *uint
  35. // Not uint
  36. c.m[name] = "-1"
  37. _, err := parseQueryUint(name, dflt, min, max, c)
  38. assert.Error(t, err)
  39. c.m[name] = "a"
  40. _, err = parseQueryUint(name, dflt, min, max, c)
  41. assert.Error(t, err)
  42. c.m[name] = "0.1"
  43. _, err = parseQueryUint(name, dflt, min, max, c)
  44. assert.Error(t, err)
  45. c.m[name] = "1.0"
  46. _, err = parseQueryUint(name, dflt, min, max, c)
  47. assert.Error(t, err)
  48. // Out of range
  49. c.m[name] = strconv.Itoa(int(min) - 1)
  50. _, err = parseQueryUint(name, dflt, min, max, c)
  51. assert.Error(t, err)
  52. c.m[name] = strconv.Itoa(int(max) + 1)
  53. _, err = parseQueryUint(name, dflt, min, max, c)
  54. assert.Error(t, err)
  55. // Default nil
  56. c.m[name] = ""
  57. res, err := parseQueryUint(name, dflt, min, max, c)
  58. assert.NoError(t, err)
  59. assert.Nil(t, res)
  60. // Default not nil
  61. dflt = new(uint)
  62. *dflt = uint(min)
  63. res, err = parseQueryUint(name, dflt, min, max, c)
  64. assert.NoError(t, err)
  65. assert.Equal(t, uint(min), *res)
  66. // Correct
  67. c.m[name] = strconv.Itoa(int(max))
  68. res, err = parseQueryUint(name, res, min, max, c)
  69. assert.NoError(t, err)
  70. assert.Equal(t, uint(max), *res)
  71. }
  72. func TestParseQueryBool(t *testing.T) {
  73. name := "foo"
  74. c := &queryParser{}
  75. c.m = make(map[string]string)
  76. var dflt *bool
  77. // Not bool
  78. c.m[name] = "x"
  79. _, err := parseQueryBool(name, dflt, c)
  80. assert.Error(t, err)
  81. c.m[name] = "False"
  82. _, err = parseQueryBool(name, dflt, c)
  83. assert.Error(t, err)
  84. c.m[name] = "0"
  85. _, err = parseQueryBool(name, dflt, c)
  86. assert.Error(t, err)
  87. c.m[name] = "1"
  88. _, err = parseQueryBool(name, dflt, c)
  89. assert.Error(t, err)
  90. // Default nil
  91. c.m[name] = ""
  92. res, err := parseQueryBool(name, dflt, c)
  93. assert.NoError(t, err)
  94. assert.Nil(t, res)
  95. // Default not nil
  96. dflt = new(bool)
  97. *dflt = true
  98. res, err = parseQueryBool(name, dflt, c)
  99. assert.NoError(t, err)
  100. assert.True(t, *res)
  101. // Correct
  102. c.m[name] = "false"
  103. res, err = parseQueryBool(name, dflt, c)
  104. assert.NoError(t, err)
  105. assert.False(t, *res)
  106. c.m[name] = "true"
  107. res, err = parseQueryBool(name, dflt, c)
  108. assert.NoError(t, err)
  109. assert.True(t, *res)
  110. }
  111. func TestParsePagination(t *testing.T) {
  112. c := &queryParser{}
  113. c.m = make(map[string]string)
  114. // fromItem out of range
  115. c.m["fromItem"] = "-1"
  116. _, _, _, err := parsePagination(c)
  117. assert.Error(t, err)
  118. c.m["fromItem"] = strconv.Itoa(maxUint32 + 1)
  119. _, _, _, err = parsePagination(c)
  120. assert.Error(t, err)
  121. c.m["fromItem"] = ""
  122. // Bad order
  123. c.m["order"] = "0"
  124. _, _, _, err = parsePagination(c)
  125. assert.Error(t, err)
  126. c.m["order"] = strconv.Itoa(int(maxLimit) + 1)
  127. _, _, _, err = parsePagination(c)
  128. assert.Error(t, err)
  129. // Default
  130. c.m["fromItem"] = ""
  131. c.m["order"] = ""
  132. c.m["limit"] = ""
  133. fromItem, order, limit, err := parsePagination(c)
  134. assert.NoError(t, err)
  135. assert.Nil(t, fromItem)
  136. assert.Equal(t, dfltOrder, order)
  137. assert.Equal(t, dfltLimit, *limit)
  138. // Correct
  139. c.m["fromItem"] = ""
  140. c.m["order"] = "ASC"
  141. c.m["limit"] = "25"
  142. fromItem, order, limit, err = parsePagination(c)
  143. assert.NoError(t, err)
  144. assert.Nil(t, fromItem)
  145. assert.Equal(t, "ASC", order)
  146. assert.Equal(t, 25, int(*limit))
  147. c.m["fromItem"] = "25"
  148. c.m["order"] = "DESC"
  149. c.m["limit"] = "50"
  150. fromItem, order, limit, err = parsePagination(c)
  151. assert.NoError(t, err)
  152. assert.Equal(t, 25, int(*fromItem))
  153. assert.Equal(t, "DESC", order)
  154. assert.Equal(t, 50, int(*limit))
  155. }
  156. func TestParseQueryHezEthAddr(t *testing.T) {
  157. name := "hezEthereumAddress"
  158. c := &queryParser{}
  159. c.m = make(map[string]string)
  160. ethAddr := ethCommon.BigToAddress(big.NewInt(int64(347683)))
  161. // Not HEZ Eth addr
  162. c.m[name] = "hez:0xf"
  163. _, err := parseQueryHezEthAddr(c)
  164. assert.Error(t, err)
  165. c.m[name] = ethAddr.String()
  166. _, err = parseQueryHezEthAddr(c)
  167. assert.Error(t, err)
  168. c.m[name] = "hez:0xXX942cfcd25ad4d90a62358b0dd84f33b39826XX"
  169. _, err = parseQueryHezEthAddr(c)
  170. assert.Error(t, err)
  171. // Default
  172. c.m[name] = ""
  173. res, err := parseQueryHezEthAddr(c)
  174. assert.NoError(t, err)
  175. assert.Nil(t, res)
  176. // Correct
  177. c.m[name] = "hez:" + ethAddr.String()
  178. res, err = parseQueryHezEthAddr(c)
  179. assert.NoError(t, err)
  180. assert.Equal(t, ethAddr, *res)
  181. }
  182. func TestParseQueryBJJ(t *testing.T) {
  183. name := "BJJ"
  184. c := &queryParser{}
  185. c.m = make(map[string]string)
  186. privK := babyjub.NewRandPrivKey()
  187. pubK := privK.Public()
  188. pkComp := [32]byte(pubK.Compress())
  189. // Not HEZ Eth addr
  190. c.m[name] = "hez:abcd"
  191. _, err := parseQueryBJJ(c)
  192. assert.Error(t, err)
  193. c.m[name] = pubK.String()
  194. _, err = parseQueryBJJ(c)
  195. assert.Error(t, err)
  196. // Wrong checksum
  197. bjjSum := append(pkComp[:], byte(1))
  198. c.m[name] = "hez:" + base64.RawStdEncoding.EncodeToString(bjjSum)
  199. _, err = parseQueryBJJ(c)
  200. assert.Error(t, err)
  201. // Default
  202. c.m[name] = ""
  203. res, err := parseQueryBJJ(c)
  204. assert.NoError(t, err)
  205. assert.Nil(t, res)
  206. // Correct
  207. c.m[name] = bjjToString(pubK.Compress())
  208. res, err = parseQueryBJJ(c)
  209. assert.NoError(t, err)
  210. assert.Equal(t, pubK.Compress(), *res)
  211. }
  212. func TestParseQueryTxType(t *testing.T) {
  213. name := "type"
  214. c := &queryParser{}
  215. c.m = make(map[string]string)
  216. // Incorrect values
  217. c.m[name] = "deposit"
  218. _, err := parseQueryTxType(c)
  219. assert.Error(t, err)
  220. c.m[name] = "1"
  221. _, err = parseQueryTxType(c)
  222. assert.Error(t, err)
  223. // Default
  224. c.m[name] = ""
  225. res, err := parseQueryTxType(c)
  226. assert.NoError(t, err)
  227. assert.Nil(t, res)
  228. // Correct values
  229. c.m[name] = string(common.TxTypeExit)
  230. res, err = parseQueryTxType(c)
  231. assert.NoError(t, err)
  232. assert.Equal(t, common.TxTypeExit, *res)
  233. c.m[name] = string(common.TxTypeTransfer)
  234. res, err = parseQueryTxType(c)
  235. assert.NoError(t, err)
  236. assert.Equal(t, common.TxTypeTransfer, *res)
  237. c.m[name] = string(common.TxTypeDeposit)
  238. res, err = parseQueryTxType(c)
  239. assert.NoError(t, err)
  240. assert.Equal(t, common.TxTypeDeposit, *res)
  241. c.m[name] = string(common.TxTypeCreateAccountDeposit)
  242. res, err = parseQueryTxType(c)
  243. assert.NoError(t, err)
  244. assert.Equal(t, common.TxTypeCreateAccountDeposit, *res)
  245. c.m[name] = string(common.TxTypeCreateAccountDepositTransfer)
  246. res, err = parseQueryTxType(c)
  247. assert.NoError(t, err)
  248. assert.Equal(t, common.TxTypeCreateAccountDepositTransfer, *res)
  249. c.m[name] = string(common.TxTypeDepositTransfer)
  250. res, err = parseQueryTxType(c)
  251. assert.NoError(t, err)
  252. assert.Equal(t, common.TxTypeDepositTransfer, *res)
  253. c.m[name] = string(common.TxTypeForceTransfer)
  254. res, err = parseQueryTxType(c)
  255. assert.NoError(t, err)
  256. assert.Equal(t, common.TxTypeForceTransfer, *res)
  257. c.m[name] = string(common.TxTypeForceExit)
  258. res, err = parseQueryTxType(c)
  259. assert.NoError(t, err)
  260. assert.Equal(t, common.TxTypeForceExit, *res)
  261. c.m[name] = string(common.TxTypeTransferToEthAddr)
  262. res, err = parseQueryTxType(c)
  263. assert.NoError(t, err)
  264. assert.Equal(t, common.TxTypeTransferToEthAddr, *res)
  265. c.m[name] = string(common.TxTypeTransferToBJJ)
  266. res, err = parseQueryTxType(c)
  267. assert.NoError(t, err)
  268. assert.Equal(t, common.TxTypeTransferToBJJ, *res)
  269. }
  270. func TestParseTokenFilters(t *testing.T) {
  271. ids := "ids"
  272. symbols := "symbols"
  273. name := "name"
  274. nameValue := "1"
  275. symbolsValue := "1,2,3"
  276. idsValue := "2,3,4"
  277. c := &queryParser{}
  278. c.m = make(map[string]string)
  279. // Incorrect values
  280. c.m[name] = nameValue
  281. c.m[ids] = idsValue
  282. c.m[symbols] = symbolsValue
  283. idsParse, symbolsParse, nameParse, err := parseTokenFilters(c)
  284. assert.NoError(t, err)
  285. // Correct values
  286. var tokenIds []common.TokenID = []common.TokenID{2, 3, 4}
  287. assert.Equal(t, tokenIds, idsParse)
  288. var symbolsArray []string = []string{"1", "2", "3"}
  289. assert.Equal(t, symbolsArray, symbolsParse)
  290. assert.Equal(t, nameValue, nameParse)
  291. }
  292. func TestParseEthAddr(t *testing.T) {
  293. name := "forgerAddr"
  294. c := &queryParser{}
  295. c.m = make(map[string]string)
  296. ethAddr := ethCommon.BigToAddress(big.NewInt(int64(123456)))
  297. // Default
  298. c.m[name] = ""
  299. res, err := parseQueryEthAddr(name, c)
  300. assert.NoError(t, err)
  301. assert.Nil(t, res)
  302. // Incorrect
  303. c.m[name] = "0x12345678"
  304. _, err = parseQueryEthAddr(name, c)
  305. assert.Error(t, err)
  306. // Correct
  307. c.m[name] = ethAddr.String()
  308. res, err = parseQueryEthAddr(name, c)
  309. assert.NoError(t, err)
  310. assert.Equal(t, ethAddr, *res)
  311. }
  312. func TestParseBidFilters(t *testing.T) {
  313. slotNum := "slotNum"
  314. bidderAddr := "bidderAddr"
  315. slotNumValue := "2"
  316. bidderAddrValue := "0xaa942cfcd25ad4d90a62358b0dd84f33b398262a"
  317. c := &queryParser{}
  318. c.m = make(map[string]string)
  319. // Incorrect values
  320. c.m[slotNum] = slotNumValue
  321. c.m[bidderAddr] = bidderAddrValue
  322. slotNumParse, bidderAddrParse, err := parseBidFilters(c)
  323. assert.NoError(t, err)
  324. // Correct values
  325. assert.Equal(t, strings.ToLower(bidderAddrParse.Hex()), bidderAddrValue)
  326. assert.Equal(t, slotNumValue, strconv.FormatUint(uint64(*slotNumParse), 10))
  327. }