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.

285 lines
8.2 KiB

  1. package apitypes
  2. import (
  3. "database/sql"
  4. "io/ioutil"
  5. "math/big"
  6. "os"
  7. "testing"
  8. ethCommon "github.com/ethereum/go-ethereum/common"
  9. dbUtils "github.com/hermeznetwork/hermez-node/db"
  10. "github.com/iden3/go-iden3-crypto/babyjub"
  11. _ "github.com/mattn/go-sqlite3" //nolint sqlite driver
  12. "github.com/russross/meddler"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. var db *sql.DB
  16. func TestMain(m *testing.M) {
  17. // Register meddler
  18. meddler.Default = meddler.SQLite
  19. meddler.Register("bigint", dbUtils.BigIntMeddler{})
  20. meddler.Register("bigintnull", dbUtils.BigIntNullMeddler{})
  21. // Create temporary sqlite DB
  22. dir, err := ioutil.TempDir("", "db")
  23. if err != nil {
  24. panic(err)
  25. }
  26. db, err = sql.Open("sqlite3", dir+"sqlite.db")
  27. if err != nil {
  28. panic(err)
  29. }
  30. defer os.RemoveAll(dir) //nolint
  31. schema := `CREATE TABLE test (i BLOB);`
  32. if _, err := db.Exec(schema); err != nil {
  33. panic(err)
  34. }
  35. // Run tests
  36. result := m.Run()
  37. os.Exit(result)
  38. }
  39. func TestBigIntStrScannerValuer(t *testing.T) {
  40. // Clean DB
  41. _, err := db.Exec("delete from test")
  42. assert.NoError(t, err)
  43. // Example structs
  44. type bigInMeddlerStruct struct {
  45. I *big.Int `meddler:"i,bigint"` // note the bigint that instructs meddler to use BigIntMeddler
  46. }
  47. type bigIntStrStruct struct {
  48. I BigIntStr `meddler:"i"` // note that no meddler is specified, and Scan/Value will be used
  49. }
  50. type bigInMeddlerStructNil struct {
  51. I *big.Int `meddler:"i,bigintnull"` // note the bigint that instructs meddler to use BigIntNullMeddler
  52. }
  53. type bigIntStrStructNil struct {
  54. I *BigIntStr `meddler:"i"` // note that no meddler is specified, and Scan/Value will be used
  55. }
  56. // Not nil case
  57. // Insert into DB using meddler
  58. const x = int64(12345)
  59. fromMeddler := bigInMeddlerStruct{
  60. I: big.NewInt(x),
  61. }
  62. err = meddler.Insert(db, "test", &fromMeddler)
  63. assert.NoError(t, err)
  64. // Read from DB using BigIntStr
  65. toBigIntStr := bigIntStrStruct{}
  66. err = meddler.QueryRow(db, &toBigIntStr, "select * from test")
  67. assert.NoError(t, err)
  68. assert.Equal(t, fromMeddler.I.String(), string(toBigIntStr.I))
  69. // Clean DB
  70. _, err = db.Exec("delete from test")
  71. assert.NoError(t, err)
  72. // Insert into DB using BigIntStr
  73. fromBigIntStr := bigIntStrStruct{
  74. I: "54321",
  75. }
  76. err = meddler.Insert(db, "test", &fromBigIntStr)
  77. assert.NoError(t, err)
  78. // Read from DB using meddler
  79. toMeddler := bigInMeddlerStruct{}
  80. err = meddler.QueryRow(db, &toMeddler, "select * from test")
  81. assert.NoError(t, err)
  82. assert.Equal(t, string(fromBigIntStr.I), toMeddler.I.String())
  83. // Nil case
  84. // Clean DB
  85. _, err = db.Exec("delete from test")
  86. assert.NoError(t, err)
  87. // Insert into DB using meddler
  88. fromMeddlerNil := bigInMeddlerStructNil{
  89. I: nil,
  90. }
  91. err = meddler.Insert(db, "test", &fromMeddlerNil)
  92. assert.NoError(t, err)
  93. // Read from DB using BigIntStr
  94. foo := BigIntStr("foo")
  95. toBigIntStrNil := bigIntStrStructNil{
  96. I: &foo, // check that this will be set to nil, not because of not being initialized
  97. }
  98. err = meddler.QueryRow(db, &toBigIntStrNil, "select * from test")
  99. assert.NoError(t, err)
  100. assert.Nil(t, toBigIntStrNil.I)
  101. // Clean DB
  102. _, err = db.Exec("delete from test")
  103. assert.NoError(t, err)
  104. // Insert into DB using BigIntStr
  105. fromBigIntStrNil := bigIntStrStructNil{
  106. I: nil,
  107. }
  108. err = meddler.Insert(db, "test", &fromBigIntStrNil)
  109. assert.NoError(t, err)
  110. // Read from DB using meddler
  111. toMeddlerNil := bigInMeddlerStructNil{
  112. I: big.NewInt(x), // check that this will be set to nil, not because of not being initialized
  113. }
  114. err = meddler.QueryRow(db, &toMeddlerNil, "select * from test")
  115. assert.NoError(t, err)
  116. assert.Nil(t, toMeddlerNil.I)
  117. }
  118. func TestHezEthAddr(t *testing.T) {
  119. // Clean DB
  120. _, err := db.Exec("delete from test")
  121. assert.NoError(t, err)
  122. // Example structs
  123. type ethAddrStruct struct {
  124. I ethCommon.Address `meddler:"i"`
  125. }
  126. type hezEthAddrStruct struct {
  127. I HezEthAddr `meddler:"i"`
  128. }
  129. type ethAddrStructNil struct {
  130. I *ethCommon.Address `meddler:"i"`
  131. }
  132. type hezEthAddrStructNil struct {
  133. I *HezEthAddr `meddler:"i"`
  134. }
  135. // Not nil case
  136. // Insert into DB using ethCommon.Address Scan/Value
  137. fromEth := ethAddrStruct{
  138. I: ethCommon.BigToAddress(big.NewInt(73737373)),
  139. }
  140. err = meddler.Insert(db, "test", &fromEth)
  141. assert.NoError(t, err)
  142. // Read from DB using HezEthAddr Scan/Value
  143. toHezEth := hezEthAddrStruct{}
  144. err = meddler.QueryRow(db, &toHezEth, "select * from test")
  145. assert.NoError(t, err)
  146. assert.Equal(t, NewHezEthAddr(fromEth.I), toHezEth.I)
  147. // Clean DB
  148. _, err = db.Exec("delete from test")
  149. assert.NoError(t, err)
  150. // Insert into DB using HezEthAddr Scan/Value
  151. fromHezEth := hezEthAddrStruct{
  152. I: NewHezEthAddr(ethCommon.BigToAddress(big.NewInt(3786872586))),
  153. }
  154. err = meddler.Insert(db, "test", &fromHezEth)
  155. assert.NoError(t, err)
  156. // Read from DB using ethCommon.Address Scan/Value
  157. toEth := ethAddrStruct{}
  158. err = meddler.QueryRow(db, &toEth, "select * from test")
  159. assert.NoError(t, err)
  160. assert.Equal(t, fromHezEth.I, NewHezEthAddr(toEth.I))
  161. // Nil case
  162. // Clean DB
  163. _, err = db.Exec("delete from test")
  164. assert.NoError(t, err)
  165. // Insert into DB using ethCommon.Address Scan/Value
  166. fromEthNil := ethAddrStructNil{
  167. I: nil,
  168. }
  169. err = meddler.Insert(db, "test", &fromEthNil)
  170. assert.NoError(t, err)
  171. // Read from DB using HezEthAddr Scan/Value
  172. foo := HezEthAddr("foo")
  173. toHezEthNil := hezEthAddrStructNil{
  174. I: &foo, // check that this will be set to nil, not because of not being initialized
  175. }
  176. err = meddler.QueryRow(db, &toHezEthNil, "select * from test")
  177. assert.NoError(t, err)
  178. assert.Nil(t, toHezEthNil.I)
  179. // Clean DB
  180. _, err = db.Exec("delete from test")
  181. assert.NoError(t, err)
  182. // Insert into DB using HezEthAddr Scan/Value
  183. fromHezEthNil := hezEthAddrStructNil{
  184. I: nil,
  185. }
  186. err = meddler.Insert(db, "test", &fromHezEthNil)
  187. assert.NoError(t, err)
  188. // Read from DB using ethCommon.Address Scan/Value
  189. fooAddr := ethCommon.BigToAddress(big.NewInt(1))
  190. toEthNil := ethAddrStructNil{
  191. I: &fooAddr, // check that this will be set to nil, not because of not being initialized
  192. }
  193. err = meddler.QueryRow(db, &toEthNil, "select * from test")
  194. assert.NoError(t, err)
  195. assert.Nil(t, toEthNil.I)
  196. }
  197. func TestHezBJJ(t *testing.T) {
  198. // Clean DB
  199. _, err := db.Exec("delete from test")
  200. assert.NoError(t, err)
  201. // Example structs
  202. type bjjStruct struct {
  203. I *babyjub.PublicKey `meddler:"i"`
  204. }
  205. type hezBJJStruct struct {
  206. I HezBJJ `meddler:"i"`
  207. }
  208. type hezBJJStructNil struct {
  209. I *HezBJJ `meddler:"i"`
  210. }
  211. // Not nil case
  212. // Insert into DB using *babyjub.PublicKey Scan/Value
  213. priv := babyjub.NewRandPrivKey()
  214. fromBJJ := bjjStruct{
  215. I: priv.Public(),
  216. }
  217. err = meddler.Insert(db, "test", &fromBJJ)
  218. assert.NoError(t, err)
  219. // Read from DB using HezBJJ Scan/Value
  220. toHezBJJ := hezBJJStruct{}
  221. err = meddler.QueryRow(db, &toHezBJJ, "select * from test")
  222. assert.NoError(t, err)
  223. assert.Equal(t, NewHezBJJ(fromBJJ.I), toHezBJJ.I)
  224. // Clean DB
  225. _, err = db.Exec("delete from test")
  226. assert.NoError(t, err)
  227. // Insert into DB using HezBJJ Scan/Value
  228. fromHezBJJ := hezBJJStruct{
  229. I: NewHezBJJ(priv.Public()),
  230. }
  231. err = meddler.Insert(db, "test", &fromHezBJJ)
  232. assert.NoError(t, err)
  233. // Read from DB using *babyjub.PublicKey Scan/Value
  234. toBJJ := bjjStruct{}
  235. err = meddler.QueryRow(db, &toBJJ, "select * from test")
  236. assert.NoError(t, err)
  237. assert.Equal(t, fromHezBJJ.I, NewHezBJJ(toBJJ.I))
  238. // Nil case
  239. // Clean DB
  240. _, err = db.Exec("delete from test")
  241. assert.NoError(t, err)
  242. // Insert into DB using *babyjub.PublicKey Scan/Value
  243. fromBJJNil := bjjStruct{
  244. I: nil,
  245. }
  246. err = meddler.Insert(db, "test", &fromBJJNil)
  247. assert.NoError(t, err)
  248. // Read from DB using HezBJJ Scan/Value
  249. foo := HezBJJ("foo")
  250. toHezBJJNil := hezBJJStructNil{
  251. I: &foo, // check that this will be set to nil, not because of not being initialized
  252. }
  253. err = meddler.QueryRow(db, &toHezBJJNil, "select * from test")
  254. assert.NoError(t, err)
  255. assert.Nil(t, toHezBJJNil.I)
  256. // Clean DB
  257. _, err = db.Exec("delete from test")
  258. assert.NoError(t, err)
  259. // Insert into DB using HezBJJ Scan/Value
  260. fromHezBJJNil := hezBJJStructNil{
  261. I: nil,
  262. }
  263. err = meddler.Insert(db, "test", &fromHezBJJNil)
  264. assert.NoError(t, err)
  265. // Read from DB using *babyjub.PublicKey Scan/Value
  266. toBJJNil := bjjStruct{
  267. I: priv.Public(), // check that this will be set to nil, not because of not being initialized
  268. }
  269. err = meddler.QueryRow(db, &toBJJNil, "select * from test")
  270. assert.NoError(t, err)
  271. assert.Nil(t, toBJJNil.I)
  272. }