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.

419 lines
12 KiB

  1. package apitypes
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "io/ioutil"
  6. "math/big"
  7. "os"
  8. "testing"
  9. ethCommon "github.com/ethereum/go-ethereum/common"
  10. "github.com/hermeznetwork/hermez-node/common"
  11. dbUtils "github.com/hermeznetwork/hermez-node/db"
  12. "github.com/iden3/go-iden3-crypto/babyjub"
  13. // nolint sqlite driver
  14. _ "github.com/mattn/go-sqlite3"
  15. "github.com/russross/meddler"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. var db *sql.DB
  19. func TestMain(m *testing.M) {
  20. // Register meddler
  21. meddler.Default = meddler.SQLite
  22. meddler.Register("bigint", dbUtils.BigIntMeddler{})
  23. meddler.Register("bigintnull", dbUtils.BigIntNullMeddler{})
  24. // Create temporary sqlite DB
  25. dir, err := ioutil.TempDir("", "db")
  26. if err != nil {
  27. panic(err)
  28. }
  29. db, err = sql.Open("sqlite3", dir+"sqlite.db")
  30. if err != nil {
  31. panic(err)
  32. }
  33. defer os.RemoveAll(dir) //nolint
  34. schema := `CREATE TABLE test (i BLOB);`
  35. if _, err := db.Exec(schema); err != nil {
  36. panic(err)
  37. }
  38. // Run tests
  39. result := m.Run()
  40. os.Exit(result)
  41. }
  42. func TestBigIntStrScannerValuer(t *testing.T) {
  43. // Clean DB
  44. _, err := db.Exec("delete from test")
  45. assert.NoError(t, err)
  46. // Example structs
  47. type bigInMeddlerStruct struct {
  48. I *big.Int `meddler:"i,bigint"` // note the bigint that instructs meddler to use BigIntMeddler
  49. }
  50. type bigIntStrStruct struct {
  51. I BigIntStr `meddler:"i"` // note that no meddler is specified, and Scan/Value will be used
  52. }
  53. type bigInMeddlerStructNil struct {
  54. I *big.Int `meddler:"i,bigintnull"` // note the bigint that instructs meddler to use BigIntNullMeddler
  55. }
  56. type bigIntStrStructNil struct {
  57. I *BigIntStr `meddler:"i"` // note that no meddler is specified, and Scan/Value will be used
  58. }
  59. // Not nil case
  60. // Insert into DB using meddler
  61. const x = int64(12345)
  62. fromMeddler := bigInMeddlerStruct{
  63. I: big.NewInt(x),
  64. }
  65. err = meddler.Insert(db, "test", &fromMeddler)
  66. assert.NoError(t, err)
  67. // Read from DB using BigIntStr
  68. toBigIntStr := bigIntStrStruct{}
  69. err = meddler.QueryRow(db, &toBigIntStr, "select * from test")
  70. assert.NoError(t, err)
  71. assert.Equal(t, fromMeddler.I.String(), string(toBigIntStr.I))
  72. // Clean DB
  73. _, err = db.Exec("delete from test")
  74. assert.NoError(t, err)
  75. // Insert into DB using BigIntStr
  76. fromBigIntStr := bigIntStrStruct{
  77. I: "54321",
  78. }
  79. err = meddler.Insert(db, "test", &fromBigIntStr)
  80. assert.NoError(t, err)
  81. // Read from DB using meddler
  82. toMeddler := bigInMeddlerStruct{}
  83. err = meddler.QueryRow(db, &toMeddler, "select * from test")
  84. assert.NoError(t, err)
  85. assert.Equal(t, string(fromBigIntStr.I), toMeddler.I.String())
  86. // Nil case
  87. // Clean DB
  88. _, err = db.Exec("delete from test")
  89. assert.NoError(t, err)
  90. // Insert into DB using meddler
  91. fromMeddlerNil := bigInMeddlerStructNil{
  92. I: nil,
  93. }
  94. err = meddler.Insert(db, "test", &fromMeddlerNil)
  95. assert.NoError(t, err)
  96. // Read from DB using BigIntStr
  97. foo := BigIntStr("foo")
  98. toBigIntStrNil := bigIntStrStructNil{
  99. I: &foo, // check that this will be set to nil, not because of not being initialized
  100. }
  101. err = meddler.QueryRow(db, &toBigIntStrNil, "select * from test")
  102. assert.NoError(t, err)
  103. assert.Nil(t, toBigIntStrNil.I)
  104. // Clean DB
  105. _, err = db.Exec("delete from test")
  106. assert.NoError(t, err)
  107. // Insert into DB using BigIntStr
  108. fromBigIntStrNil := bigIntStrStructNil{
  109. I: nil,
  110. }
  111. err = meddler.Insert(db, "test", &fromBigIntStrNil)
  112. assert.NoError(t, err)
  113. // Read from DB using meddler
  114. toMeddlerNil := bigInMeddlerStructNil{
  115. I: big.NewInt(x), // check that this will be set to nil, not because of not being initialized
  116. }
  117. err = meddler.QueryRow(db, &toMeddlerNil, "select * from test")
  118. assert.NoError(t, err)
  119. assert.Nil(t, toMeddlerNil.I)
  120. }
  121. func TestStrBigInt(t *testing.T) {
  122. type testStrBigInt struct {
  123. I StrBigInt
  124. }
  125. from := []byte(`{"I":"4"}`)
  126. to := &testStrBigInt{}
  127. assert.NoError(t, json.Unmarshal(from, to))
  128. assert.Equal(t, big.NewInt(4), (*big.Int)(&to.I))
  129. }
  130. func TestStrHezEthAddr(t *testing.T) {
  131. type testStrHezEthAddr struct {
  132. I StrHezEthAddr
  133. }
  134. withoutHez := "0xaa942cfcd25ad4d90a62358b0dd84f33b398262a"
  135. from := []byte(`{"I":"hez:` + withoutHez + `"}`)
  136. var addr ethCommon.Address
  137. if err := addr.UnmarshalText([]byte(withoutHez)); err != nil {
  138. panic(err)
  139. }
  140. to := &testStrHezEthAddr{}
  141. assert.NoError(t, json.Unmarshal(from, to))
  142. assert.Equal(t, addr, ethCommon.Address(to.I))
  143. }
  144. func TestStrHezBJJ(t *testing.T) {
  145. type testStrHezBJJ struct {
  146. I StrHezBJJ
  147. }
  148. priv := babyjub.NewRandPrivKey()
  149. hezBjj := NewHezBJJ(priv.Public().Compress())
  150. from := []byte(`{"I":"` + hezBjj + `"}`)
  151. to := &testStrHezBJJ{}
  152. assert.NoError(t, json.Unmarshal(from, to))
  153. assert.Equal(t, priv.Public().Compress(), (babyjub.PublicKeyComp)(to.I))
  154. }
  155. func TestStrHezIdx(t *testing.T) {
  156. type testStrHezIdx struct {
  157. I StrHezIdx
  158. }
  159. from := []byte(`{"I":"hez:foo:4"}`)
  160. to := &testStrHezIdx{}
  161. assert.NoError(t, json.Unmarshal(from, to))
  162. assert.Equal(t, common.Idx(4), common.Idx(to.I))
  163. }
  164. func TestHezEthAddr(t *testing.T) {
  165. // Clean DB
  166. _, err := db.Exec("delete from test")
  167. assert.NoError(t, err)
  168. // Example structs
  169. type ethAddrStruct struct {
  170. I ethCommon.Address `meddler:"i"`
  171. }
  172. type hezEthAddrStruct struct {
  173. I HezEthAddr `meddler:"i"`
  174. }
  175. type ethAddrStructNil struct {
  176. I *ethCommon.Address `meddler:"i"`
  177. }
  178. type hezEthAddrStructNil struct {
  179. I *HezEthAddr `meddler:"i"`
  180. }
  181. // Not nil case
  182. // Insert into DB using ethCommon.Address Scan/Value
  183. fromEth := ethAddrStruct{
  184. I: ethCommon.BigToAddress(big.NewInt(73737373)),
  185. }
  186. err = meddler.Insert(db, "test", &fromEth)
  187. assert.NoError(t, err)
  188. // Read from DB using HezEthAddr Scan/Value
  189. toHezEth := hezEthAddrStruct{}
  190. err = meddler.QueryRow(db, &toHezEth, "select * from test")
  191. assert.NoError(t, err)
  192. assert.Equal(t, NewHezEthAddr(fromEth.I), toHezEth.I)
  193. // Clean DB
  194. _, err = db.Exec("delete from test")
  195. assert.NoError(t, err)
  196. // Insert into DB using HezEthAddr Scan/Value
  197. fromHezEth := hezEthAddrStruct{
  198. I: NewHezEthAddr(ethCommon.BigToAddress(big.NewInt(3786872586))),
  199. }
  200. err = meddler.Insert(db, "test", &fromHezEth)
  201. assert.NoError(t, err)
  202. // Read from DB using ethCommon.Address Scan/Value
  203. toEth := ethAddrStruct{}
  204. err = meddler.QueryRow(db, &toEth, "select * from test")
  205. assert.NoError(t, err)
  206. assert.Equal(t, fromHezEth.I, NewHezEthAddr(toEth.I))
  207. // Nil case
  208. // Clean DB
  209. _, err = db.Exec("delete from test")
  210. assert.NoError(t, err)
  211. // Insert into DB using ethCommon.Address Scan/Value
  212. fromEthNil := ethAddrStructNil{
  213. I: nil,
  214. }
  215. err = meddler.Insert(db, "test", &fromEthNil)
  216. assert.NoError(t, err)
  217. // Read from DB using HezEthAddr Scan/Value
  218. foo := HezEthAddr("foo")
  219. toHezEthNil := hezEthAddrStructNil{
  220. I: &foo, // check that this will be set to nil, not because of not being initialized
  221. }
  222. err = meddler.QueryRow(db, &toHezEthNil, "select * from test")
  223. assert.NoError(t, err)
  224. assert.Nil(t, toHezEthNil.I)
  225. // Clean DB
  226. _, err = db.Exec("delete from test")
  227. assert.NoError(t, err)
  228. // Insert into DB using HezEthAddr Scan/Value
  229. fromHezEthNil := hezEthAddrStructNil{
  230. I: nil,
  231. }
  232. err = meddler.Insert(db, "test", &fromHezEthNil)
  233. assert.NoError(t, err)
  234. // Read from DB using ethCommon.Address Scan/Value
  235. fooAddr := ethCommon.BigToAddress(big.NewInt(1))
  236. toEthNil := ethAddrStructNil{
  237. I: &fooAddr, // check that this will be set to nil, not because of not being initialized
  238. }
  239. err = meddler.QueryRow(db, &toEthNil, "select * from test")
  240. assert.NoError(t, err)
  241. assert.Nil(t, toEthNil.I)
  242. }
  243. func TestHezBJJ(t *testing.T) {
  244. // Clean DB
  245. _, err := db.Exec("delete from test")
  246. assert.NoError(t, err)
  247. // Example structs
  248. type bjjStruct struct {
  249. I babyjub.PublicKeyComp `meddler:"i"`
  250. }
  251. type hezBJJStruct struct {
  252. I HezBJJ `meddler:"i"`
  253. }
  254. type bjjStructNil struct {
  255. I *babyjub.PublicKeyComp `meddler:"i"`
  256. }
  257. type hezBJJStructNil struct {
  258. I *HezBJJ `meddler:"i"`
  259. }
  260. // Not nil case
  261. // Insert into DB using *babyjub.PublicKeyComp Scan/Value
  262. priv := babyjub.NewRandPrivKey()
  263. fromBJJ := bjjStruct{
  264. I: priv.Public().Compress(),
  265. }
  266. err = meddler.Insert(db, "test", &fromBJJ)
  267. assert.NoError(t, err)
  268. // Read from DB using HezBJJ Scan/Value
  269. toHezBJJ := hezBJJStruct{}
  270. err = meddler.QueryRow(db, &toHezBJJ, "select * from test")
  271. assert.NoError(t, err)
  272. assert.Equal(t, NewHezBJJ(fromBJJ.I), toHezBJJ.I)
  273. // Clean DB
  274. _, err = db.Exec("delete from test")
  275. assert.NoError(t, err)
  276. // Insert into DB using HezBJJ Scan/Value
  277. fromHezBJJ := hezBJJStruct{
  278. I: NewHezBJJ(priv.Public().Compress()),
  279. }
  280. err = meddler.Insert(db, "test", &fromHezBJJ)
  281. assert.NoError(t, err)
  282. // Read from DB using *babyjub.PublicKeyComp Scan/Value
  283. toBJJ := bjjStruct{}
  284. err = meddler.QueryRow(db, &toBJJ, "select * from test")
  285. assert.NoError(t, err)
  286. assert.Equal(t, fromHezBJJ.I, NewHezBJJ(toBJJ.I))
  287. // Nil case
  288. // Clean DB
  289. _, err = db.Exec("delete from test")
  290. assert.NoError(t, err)
  291. // Insert into DB using *babyjub.PublicKeyComp Scan/Value
  292. fromBJJNil := bjjStructNil{
  293. I: nil,
  294. }
  295. err = meddler.Insert(db, "test", &fromBJJNil)
  296. assert.NoError(t, err)
  297. // Read from DB using HezBJJ Scan/Value
  298. foo := HezBJJ("foo")
  299. toHezBJJNil := hezBJJStructNil{
  300. I: &foo, // check that this will be set to nil, not because of not being initialized
  301. }
  302. err = meddler.QueryRow(db, &toHezBJJNil, "select * from test")
  303. assert.NoError(t, err)
  304. assert.Nil(t, toHezBJJNil.I)
  305. // Clean DB
  306. _, err = db.Exec("delete from test")
  307. assert.NoError(t, err)
  308. // Insert into DB using HezBJJ Scan/Value
  309. fromHezBJJNil := hezBJJStructNil{
  310. I: nil,
  311. }
  312. err = meddler.Insert(db, "test", &fromHezBJJNil)
  313. assert.NoError(t, err)
  314. // Read from DB using *babyjub.PublicKeyComp Scan/Value
  315. bjjComp := priv.Public().Compress()
  316. toBJJNil := bjjStructNil{
  317. I: &bjjComp, // check that this will be set to nil, not because of not being initialized
  318. }
  319. err = meddler.QueryRow(db, &toBJJNil, "select * from test")
  320. assert.NoError(t, err)
  321. assert.Nil(t, toBJJNil.I)
  322. }
  323. func TestEthSignature(t *testing.T) {
  324. // Clean DB
  325. _, err := db.Exec("delete from test")
  326. assert.NoError(t, err)
  327. // Example structs
  328. type ethSignStruct struct {
  329. I []byte `meddler:"i"`
  330. }
  331. type hezEthSignStruct struct {
  332. I EthSignature `meddler:"i"`
  333. }
  334. type hezEthSignStructNil struct {
  335. I *EthSignature `meddler:"i"`
  336. }
  337. // Not nil case
  338. // Insert into DB using []byte Scan/Value
  339. s := "someRandomFooForYou"
  340. fromEth := ethSignStruct{
  341. I: []byte(s),
  342. }
  343. err = meddler.Insert(db, "test", &fromEth)
  344. assert.NoError(t, err)
  345. // Read from DB using EthSignature Scan/Value
  346. toHezEth := hezEthSignStruct{}
  347. err = meddler.QueryRow(db, &toHezEth, "select * from test")
  348. assert.NoError(t, err)
  349. assert.Equal(t, NewEthSignature(fromEth.I), &toHezEth.I)
  350. // Clean DB
  351. _, err = db.Exec("delete from test")
  352. assert.NoError(t, err)
  353. // Insert into DB using EthSignature Scan/Value
  354. fromHezEth := hezEthSignStruct{
  355. I: *NewEthSignature([]byte(s)),
  356. }
  357. err = meddler.Insert(db, "test", &fromHezEth)
  358. assert.NoError(t, err)
  359. // Read from DB using []byte Scan/Value
  360. toEth := ethSignStruct{}
  361. err = meddler.QueryRow(db, &toEth, "select * from test")
  362. assert.NoError(t, err)
  363. assert.Equal(t, &fromHezEth.I, NewEthSignature(toEth.I))
  364. // Nil case
  365. // Clean DB
  366. _, err = db.Exec("delete from test")
  367. assert.NoError(t, err)
  368. // Insert into DB using []byte Scan/Value
  369. fromEthNil := ethSignStruct{
  370. I: nil,
  371. }
  372. err = meddler.Insert(db, "test", &fromEthNil)
  373. assert.NoError(t, err)
  374. // Read from DB using EthSignature Scan/Value
  375. foo := EthSignature("foo")
  376. toHezEthNil := hezEthSignStructNil{
  377. I: &foo, // check that this will be set to nil, not because of not being initialized
  378. }
  379. err = meddler.QueryRow(db, &toHezEthNil, "select * from test")
  380. assert.NoError(t, err)
  381. assert.Nil(t, toHezEthNil.I)
  382. // Clean DB
  383. _, err = db.Exec("delete from test")
  384. assert.NoError(t, err)
  385. // Insert into DB using EthSignature Scan/Value
  386. fromHezEthNil := hezEthSignStructNil{
  387. I: nil,
  388. }
  389. err = meddler.Insert(db, "test", &fromHezEthNil)
  390. assert.NoError(t, err)
  391. // Read from DB using []byte Scan/Value
  392. toEthNil := ethSignStruct{
  393. I: []byte(s), // check that this will be set to nil, not because of not being initialized
  394. }
  395. err = meddler.QueryRow(db, &toEthNil, "select * from test")
  396. assert.NoError(t, err)
  397. assert.Nil(t, toEthNil.I)
  398. }