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.

324 lines
8.4 KiB

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