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.

415 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())
  150. from := []byte(`{"I":"` + hezBjj + `"}`)
  151. to := &testStrHezBJJ{}
  152. assert.NoError(t, json.Unmarshal(from, to))
  153. assert.Equal(t, priv.Public(), (*babyjub.PublicKey)(&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.PublicKey `meddler:"i"`
  250. }
  251. type hezBJJStruct struct {
  252. I HezBJJ `meddler:"i"`
  253. }
  254. type hezBJJStructNil struct {
  255. I *HezBJJ `meddler:"i"`
  256. }
  257. // Not nil case
  258. // Insert into DB using *babyjub.PublicKey Scan/Value
  259. priv := babyjub.NewRandPrivKey()
  260. fromBJJ := bjjStruct{
  261. I: priv.Public(),
  262. }
  263. err = meddler.Insert(db, "test", &fromBJJ)
  264. assert.NoError(t, err)
  265. // Read from DB using HezBJJ Scan/Value
  266. toHezBJJ := hezBJJStruct{}
  267. err = meddler.QueryRow(db, &toHezBJJ, "select * from test")
  268. assert.NoError(t, err)
  269. assert.Equal(t, NewHezBJJ(fromBJJ.I), toHezBJJ.I)
  270. // Clean DB
  271. _, err = db.Exec("delete from test")
  272. assert.NoError(t, err)
  273. // Insert into DB using HezBJJ Scan/Value
  274. fromHezBJJ := hezBJJStruct{
  275. I: NewHezBJJ(priv.Public()),
  276. }
  277. err = meddler.Insert(db, "test", &fromHezBJJ)
  278. assert.NoError(t, err)
  279. // Read from DB using *babyjub.PublicKey Scan/Value
  280. toBJJ := bjjStruct{}
  281. err = meddler.QueryRow(db, &toBJJ, "select * from test")
  282. assert.NoError(t, err)
  283. assert.Equal(t, fromHezBJJ.I, NewHezBJJ(toBJJ.I))
  284. // Nil case
  285. // Clean DB
  286. _, err = db.Exec("delete from test")
  287. assert.NoError(t, err)
  288. // Insert into DB using *babyjub.PublicKey Scan/Value
  289. fromBJJNil := bjjStruct{
  290. I: nil,
  291. }
  292. err = meddler.Insert(db, "test", &fromBJJNil)
  293. assert.NoError(t, err)
  294. // Read from DB using HezBJJ Scan/Value
  295. foo := HezBJJ("foo")
  296. toHezBJJNil := hezBJJStructNil{
  297. I: &foo, // check that this will be set to nil, not because of not being initialized
  298. }
  299. err = meddler.QueryRow(db, &toHezBJJNil, "select * from test")
  300. assert.NoError(t, err)
  301. assert.Nil(t, toHezBJJNil.I)
  302. // Clean DB
  303. _, err = db.Exec("delete from test")
  304. assert.NoError(t, err)
  305. // Insert into DB using HezBJJ Scan/Value
  306. fromHezBJJNil := hezBJJStructNil{
  307. I: nil,
  308. }
  309. err = meddler.Insert(db, "test", &fromHezBJJNil)
  310. assert.NoError(t, err)
  311. // Read from DB using *babyjub.PublicKey Scan/Value
  312. toBJJNil := bjjStruct{
  313. I: priv.Public(), // check that this will be set to nil, not because of not being initialized
  314. }
  315. err = meddler.QueryRow(db, &toBJJNil, "select * from test")
  316. assert.NoError(t, err)
  317. assert.Nil(t, toBJJNil.I)
  318. }
  319. func TestEthSignature(t *testing.T) {
  320. // Clean DB
  321. _, err := db.Exec("delete from test")
  322. assert.NoError(t, err)
  323. // Example structs
  324. type ethSignStruct struct {
  325. I []byte `meddler:"i"`
  326. }
  327. type hezEthSignStruct struct {
  328. I EthSignature `meddler:"i"`
  329. }
  330. type hezEthSignStructNil struct {
  331. I *EthSignature `meddler:"i"`
  332. }
  333. // Not nil case
  334. // Insert into DB using []byte Scan/Value
  335. s := "someRandomFooForYou"
  336. fromEth := ethSignStruct{
  337. I: []byte(s),
  338. }
  339. err = meddler.Insert(db, "test", &fromEth)
  340. assert.NoError(t, err)
  341. // Read from DB using EthSignature Scan/Value
  342. toHezEth := hezEthSignStruct{}
  343. err = meddler.QueryRow(db, &toHezEth, "select * from test")
  344. assert.NoError(t, err)
  345. assert.Equal(t, NewEthSignature(fromEth.I), &toHezEth.I)
  346. // Clean DB
  347. _, err = db.Exec("delete from test")
  348. assert.NoError(t, err)
  349. // Insert into DB using EthSignature Scan/Value
  350. fromHezEth := hezEthSignStruct{
  351. I: *NewEthSignature([]byte(s)),
  352. }
  353. err = meddler.Insert(db, "test", &fromHezEth)
  354. assert.NoError(t, err)
  355. // Read from DB using []byte Scan/Value
  356. toEth := ethSignStruct{}
  357. err = meddler.QueryRow(db, &toEth, "select * from test")
  358. assert.NoError(t, err)
  359. assert.Equal(t, &fromHezEth.I, NewEthSignature(toEth.I))
  360. // Nil case
  361. // Clean DB
  362. _, err = db.Exec("delete from test")
  363. assert.NoError(t, err)
  364. // Insert into DB using []byte Scan/Value
  365. fromEthNil := ethSignStruct{
  366. I: nil,
  367. }
  368. err = meddler.Insert(db, "test", &fromEthNil)
  369. assert.NoError(t, err)
  370. // Read from DB using EthSignature Scan/Value
  371. foo := EthSignature("foo")
  372. toHezEthNil := hezEthSignStructNil{
  373. I: &foo, // check that this will be set to nil, not because of not being initialized
  374. }
  375. err = meddler.QueryRow(db, &toHezEthNil, "select * from test")
  376. assert.NoError(t, err)
  377. assert.Nil(t, toHezEthNil.I)
  378. // Clean DB
  379. _, err = db.Exec("delete from test")
  380. assert.NoError(t, err)
  381. // Insert into DB using EthSignature Scan/Value
  382. fromHezEthNil := hezEthSignStructNil{
  383. I: nil,
  384. }
  385. err = meddler.Insert(db, "test", &fromHezEthNil)
  386. assert.NoError(t, err)
  387. // Read from DB using []byte Scan/Value
  388. toEthNil := ethSignStruct{
  389. I: []byte(s), // check that this will be set to nil, not because of not being initialized
  390. }
  391. err = meddler.QueryRow(db, &toEthNil, "select * from test")
  392. assert.NoError(t, err)
  393. assert.Nil(t, toEthNil.I)
  394. }