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.

278 lines
7.3 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. // Offset out of range
  108. c.m["offset"] = "-1"
  109. _, _, _, err := parsePagination(c)
  110. assert.Error(t, err)
  111. c.m["offset"] = strconv.Itoa(maxUint32 + 1)
  112. _, _, _, err = parsePagination(c)
  113. assert.Error(t, err)
  114. c.m["offset"] = ""
  115. // Limit out of range
  116. c.m["limit"] = "0"
  117. _, _, _, err = parsePagination(c)
  118. assert.Error(t, err)
  119. c.m["limit"] = strconv.Itoa(int(maxLimit) + 1)
  120. _, _, _, err = parsePagination(c)
  121. assert.Error(t, err)
  122. c.m["limit"] = ""
  123. // Last and offset
  124. c.m["offset"] = "1"
  125. c.m["last"] = "true"
  126. _, _, _, err = parsePagination(c)
  127. assert.Error(t, err)
  128. // Default
  129. c.m["offset"] = ""
  130. c.m["last"] = ""
  131. c.m["limit"] = ""
  132. offset, last, limit, err := parsePagination(c)
  133. assert.NoError(t, err)
  134. assert.Equal(t, 0, int(*offset))
  135. assert.Equal(t, dfltLast, *last)
  136. assert.Equal(t, dfltLimit, *limit)
  137. // Correct
  138. c.m["offset"] = ""
  139. c.m["last"] = "true"
  140. c.m["limit"] = "25"
  141. offset, last, limit, err = parsePagination(c)
  142. assert.NoError(t, err)
  143. assert.Equal(t, 0, int(*offset))
  144. assert.True(t, *last)
  145. assert.Equal(t, 25, int(*limit))
  146. c.m["offset"] = "25"
  147. c.m["last"] = "false"
  148. c.m["limit"] = "50"
  149. offset, last, limit, err = parsePagination(c)
  150. assert.NoError(t, err)
  151. assert.Equal(t, 25, int(*offset))
  152. assert.False(t, *last)
  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. }