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.

272 lines
7.2 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 TestParseQueryUint(t *testing.T) {
  22. name := "foo"
  23. c := &queryParser{}
  24. c.m = make(map[string]string)
  25. var min uint = 1
  26. var max uint = 10
  27. var dflt *uint
  28. // Not uint
  29. c.m[name] = "-1"
  30. _, err := parseQueryUint(name, dflt, min, max, c)
  31. assert.Error(t, err)
  32. c.m[name] = "a"
  33. _, err = parseQueryUint(name, dflt, min, max, c)
  34. assert.Error(t, err)
  35. c.m[name] = "0.1"
  36. _, err = parseQueryUint(name, dflt, min, max, c)
  37. assert.Error(t, err)
  38. c.m[name] = "1.0"
  39. _, err = parseQueryUint(name, dflt, min, max, c)
  40. assert.Error(t, err)
  41. // Out of range
  42. c.m[name] = strconv.Itoa(int(min) - 1)
  43. _, err = parseQueryUint(name, dflt, min, max, c)
  44. assert.Error(t, err)
  45. c.m[name] = strconv.Itoa(int(max) + 1)
  46. _, err = parseQueryUint(name, dflt, min, max, c)
  47. assert.Error(t, err)
  48. // Default nil
  49. c.m[name] = ""
  50. res, err := parseQueryUint(name, dflt, min, max, c)
  51. assert.NoError(t, err)
  52. assert.Nil(t, res)
  53. // Default not nil
  54. dflt = new(uint)
  55. *dflt = uint(min)
  56. res, err = parseQueryUint(name, dflt, min, max, c)
  57. assert.NoError(t, err)
  58. assert.Equal(t, uint(min), *res)
  59. // Correct
  60. c.m[name] = strconv.Itoa(int(max))
  61. res, err = parseQueryUint(name, res, min, max, c)
  62. assert.NoError(t, err)
  63. assert.Equal(t, uint(max), *res)
  64. }
  65. func TestParseQueryBool(t *testing.T) {
  66. name := "foo"
  67. c := &queryParser{}
  68. c.m = make(map[string]string)
  69. var dflt *bool
  70. // Not bool
  71. c.m[name] = "x"
  72. _, err := parseQueryBool(name, dflt, c)
  73. assert.Error(t, err)
  74. c.m[name] = "False"
  75. _, err = parseQueryBool(name, dflt, c)
  76. assert.Error(t, err)
  77. c.m[name] = "0"
  78. _, err = parseQueryBool(name, dflt, c)
  79. assert.Error(t, err)
  80. c.m[name] = "1"
  81. _, err = parseQueryBool(name, dflt, c)
  82. assert.Error(t, err)
  83. // Default nil
  84. c.m[name] = ""
  85. res, err := parseQueryBool(name, dflt, c)
  86. assert.NoError(t, err)
  87. assert.Nil(t, res)
  88. // Default not nil
  89. dflt = new(bool)
  90. *dflt = true
  91. res, err = parseQueryBool(name, dflt, c)
  92. assert.NoError(t, err)
  93. assert.True(t, *res)
  94. // Correct
  95. c.m[name] = "false"
  96. res, err = parseQueryBool(name, dflt, c)
  97. assert.NoError(t, err)
  98. assert.False(t, *res)
  99. c.m[name] = "true"
  100. res, err = parseQueryBool(name, dflt, c)
  101. assert.NoError(t, err)
  102. assert.True(t, *res)
  103. }
  104. func TestParsePagination(t *testing.T) {
  105. c := &queryParser{}
  106. c.m = make(map[string]string)
  107. // fromItem out of range
  108. c.m["fromItem"] = "-1"
  109. _, _, _, err := parsePagination(c)
  110. assert.Error(t, err)
  111. c.m["fromItem"] = strconv.Itoa(maxUint32 + 1)
  112. _, _, _, err = parsePagination(c)
  113. assert.Error(t, err)
  114. c.m["fromItem"] = ""
  115. // Bad order
  116. c.m["order"] = "0"
  117. _, _, _, err = parsePagination(c)
  118. assert.Error(t, err)
  119. c.m["order"] = strconv.Itoa(int(maxLimit) + 1)
  120. _, _, _, err = parsePagination(c)
  121. assert.Error(t, err)
  122. // Default
  123. c.m["fromItem"] = ""
  124. c.m["order"] = ""
  125. c.m["limit"] = ""
  126. fromItem, order, limit, err := parsePagination(c)
  127. assert.NoError(t, err)
  128. assert.Nil(t, fromItem)
  129. assert.Equal(t, dfltOrder, order)
  130. assert.Equal(t, dfltLimit, *limit)
  131. // Correct
  132. c.m["fromItem"] = ""
  133. c.m["order"] = "ASC"
  134. c.m["limit"] = "25"
  135. fromItem, order, limit, err = parsePagination(c)
  136. assert.NoError(t, err)
  137. assert.Nil(t, fromItem)
  138. assert.Equal(t, "ASC", order)
  139. assert.Equal(t, 25, int(*limit))
  140. c.m["fromItem"] = "25"
  141. c.m["order"] = "DESC"
  142. c.m["limit"] = "50"
  143. fromItem, order, limit, err = parsePagination(c)
  144. assert.NoError(t, err)
  145. assert.Equal(t, 25, int(*fromItem))
  146. assert.Equal(t, "DESC", order)
  147. assert.Equal(t, 50, int(*limit))
  148. }
  149. func TestParseQueryHezEthAddr(t *testing.T) {
  150. name := "hermezEthereumAddress"
  151. c := &queryParser{}
  152. c.m = make(map[string]string)
  153. ethAddr := ethCommon.BigToAddress(big.NewInt(int64(347683)))
  154. // Not HEZ Eth addr
  155. c.m[name] = "hez:0xf"
  156. _, err := parseQueryHezEthAddr(c)
  157. assert.Error(t, err)
  158. c.m[name] = ethAddr.String()
  159. _, err = parseQueryHezEthAddr(c)
  160. assert.Error(t, err)
  161. c.m[name] = "hez:0xXX942cfcd25ad4d90a62358b0dd84f33b39826XX"
  162. _, err = parseQueryHezEthAddr(c)
  163. assert.Error(t, err)
  164. // Default
  165. c.m[name] = ""
  166. res, err := parseQueryHezEthAddr(c)
  167. assert.NoError(t, err)
  168. assert.Nil(t, res)
  169. // Correct
  170. c.m[name] = "hez:" + ethAddr.String()
  171. res, err = parseQueryHezEthAddr(c)
  172. assert.NoError(t, err)
  173. assert.Equal(t, ethAddr, *res)
  174. }
  175. func TestParseQueryBJJ(t *testing.T) {
  176. name := "BJJ"
  177. c := &queryParser{}
  178. c.m = make(map[string]string)
  179. privK := babyjub.NewRandPrivKey()
  180. pubK := privK.Public()
  181. pkComp := [32]byte(pubK.Compress())
  182. // Not HEZ Eth addr
  183. c.m[name] = "hez:abcd"
  184. _, err := parseQueryBJJ(c)
  185. assert.Error(t, err)
  186. c.m[name] = pubK.String()
  187. _, err = parseQueryBJJ(c)
  188. assert.Error(t, err)
  189. // Wrong checksum
  190. bjjSum := append(pkComp[:], byte(1))
  191. c.m[name] = "hez:" + base64.RawStdEncoding.EncodeToString(bjjSum)
  192. _, err = parseQueryBJJ(c)
  193. assert.Error(t, err)
  194. // Default
  195. c.m[name] = ""
  196. res, err := parseQueryBJJ(c)
  197. assert.NoError(t, err)
  198. assert.Nil(t, res)
  199. // Correct
  200. c.m[name] = bjjToString(pubK)
  201. res, err = parseQueryBJJ(c)
  202. assert.NoError(t, err)
  203. assert.Equal(t, *pubK, *res)
  204. }
  205. func TestParseQueryTxType(t *testing.T) {
  206. name := "type"
  207. c := &queryParser{}
  208. c.m = make(map[string]string)
  209. // Incorrect values
  210. c.m[name] = "deposit"
  211. _, err := parseQueryTxType(c)
  212. assert.Error(t, err)
  213. c.m[name] = "1"
  214. _, err = parseQueryTxType(c)
  215. assert.Error(t, err)
  216. // Default
  217. c.m[name] = ""
  218. res, err := parseQueryTxType(c)
  219. assert.NoError(t, err)
  220. assert.Nil(t, res)
  221. // Correct values
  222. c.m[name] = string(common.TxTypeExit)
  223. res, err = parseQueryTxType(c)
  224. assert.NoError(t, err)
  225. assert.Equal(t, common.TxTypeExit, *res)
  226. c.m[name] = string(common.TxTypeTransfer)
  227. res, err = parseQueryTxType(c)
  228. assert.NoError(t, err)
  229. assert.Equal(t, common.TxTypeTransfer, *res)
  230. c.m[name] = string(common.TxTypeDeposit)
  231. res, err = parseQueryTxType(c)
  232. assert.NoError(t, err)
  233. assert.Equal(t, common.TxTypeDeposit, *res)
  234. c.m[name] = string(common.TxTypeCreateAccountDeposit)
  235. res, err = parseQueryTxType(c)
  236. assert.NoError(t, err)
  237. assert.Equal(t, common.TxTypeCreateAccountDeposit, *res)
  238. c.m[name] = string(common.TxTypeCreateAccountDepositTransfer)
  239. res, err = parseQueryTxType(c)
  240. assert.NoError(t, err)
  241. assert.Equal(t, common.TxTypeCreateAccountDepositTransfer, *res)
  242. c.m[name] = string(common.TxTypeDepositTransfer)
  243. res, err = parseQueryTxType(c)
  244. assert.NoError(t, err)
  245. assert.Equal(t, common.TxTypeDepositTransfer, *res)
  246. c.m[name] = string(common.TxTypeForceTransfer)
  247. res, err = parseQueryTxType(c)
  248. assert.NoError(t, err)
  249. assert.Equal(t, common.TxTypeForceTransfer, *res)
  250. c.m[name] = string(common.TxTypeForceExit)
  251. res, err = parseQueryTxType(c)
  252. assert.NoError(t, err)
  253. assert.Equal(t, common.TxTypeForceExit, *res)
  254. c.m[name] = string(common.TxTypeTransferToEthAddr)
  255. res, err = parseQueryTxType(c)
  256. assert.NoError(t, err)
  257. assert.Equal(t, common.TxTypeTransferToEthAddr, *res)
  258. c.m[name] = string(common.TxTypeTransferToBJJ)
  259. res, err = parseQueryTxType(c)
  260. assert.NoError(t, err)
  261. assert.Equal(t, common.TxTypeTransferToBJJ, *res)
  262. }