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.

225 lines
7.4 KiB

  1. //nolint:gomnd,golint
  2. package test
  3. import (
  4. "github.com/iden3/go-merkletree"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. // TestReturnKnownErrIfNotExists checks that the implementation of the
  9. // db.Storage interface returns the expected error in the case that the value
  10. // is not found
  11. func TestReturnKnownErrIfNotExists(t *testing.T, sto merkletree.Storage) {
  12. k := []byte("key")
  13. tx, err := sto.NewTx()
  14. //defer func() {
  15. // tx.Close()
  16. // sto.Close()
  17. //}()
  18. assert.Nil(t, err)
  19. _, err = tx.Get(k)
  20. assert.EqualError(t, err, merkletree.ErrNotFound.Error())
  21. }
  22. // TestStorageInsertGet checks that the implementation of the db.Storage
  23. // interface behaves as expected
  24. func TestStorageInsertGet(t *testing.T, sto merkletree.Storage) {
  25. key := []byte("key")
  26. value := merkletree.Hash{1, 1, 1, 1}
  27. tx, err := sto.NewTx()
  28. //defer func() {
  29. // tx.Close()
  30. // sto.Close()
  31. //}()
  32. assert.Nil(t, err)
  33. node := merkletree.NewNodeMiddle(&value, &value)
  34. err = tx.Put(key, node)
  35. assert.Nil(t, err)
  36. v, err := tx.Get(key)
  37. assert.Nil(t, err)
  38. assert.Equal(t, value, *v.ChildL)
  39. assert.Equal(t, value, *v.ChildR)
  40. assert.Nil(t, tx.Commit())
  41. tx, err = sto.NewTx()
  42. assert.Nil(t, err)
  43. v, err = tx.Get(key)
  44. assert.Nil(t, err)
  45. assert.Equal(t, value, *v.ChildL)
  46. assert.Equal(t, value, *v.ChildR)
  47. }
  48. // TestStorageWithPrefix checks that the implementation of the db.Storage
  49. // interface behaves as expected for the WithPrefix method
  50. func TestStorageWithPrefix(t *testing.T, sto merkletree.Storage) {
  51. k := []byte{9}
  52. sto1 := sto.WithPrefix([]byte{1})
  53. sto2 := sto.WithPrefix([]byte{2})
  54. // check within tx
  55. sto1tx, err := sto1.NewTx()
  56. assert.Nil(t, err)
  57. node := merkletree.NewNodeLeaf(&merkletree.Hash{1, 2, 3}, &merkletree.Hash{4, 5, 6})
  58. err = sto1tx.Put(k, node)
  59. assert.Nil(t, err)
  60. v1, err := sto1tx.Get(k)
  61. assert.Nil(t, err)
  62. assert.Equal(t, merkletree.Hash{4, 5, 6}, *v1.Entry[1])
  63. assert.Nil(t, sto1tx.Commit())
  64. sto2tx, err := sto2.NewTx()
  65. assert.Nil(t, err)
  66. node.Entry[1] = &merkletree.Hash{9, 10}
  67. err = sto2tx.Put(k, node)
  68. assert.Nil(t, err)
  69. v2, err := sto2tx.Get(k)
  70. assert.Nil(t, err)
  71. assert.Equal(t, merkletree.Hash{9, 10}, *v2.Entry[1])
  72. assert.Nil(t, sto2tx.Commit())
  73. // check outside tx
  74. v1, err = sto1.Get(k)
  75. assert.Nil(t, err)
  76. assert.Equal(t, merkletree.Hash{4, 5, 6}, *v1.Entry[1])
  77. v2, err = sto2.Get(k)
  78. assert.Nil(t, err)
  79. assert.Equal(t, merkletree.Hash{9, 10}, *v2.Entry[1])
  80. }
  81. // TestIterate checks that the implementation of the db.Storage interface
  82. // behaves as expected for the Iterate method
  83. func TestIterate(t *testing.T, sto merkletree.Storage) {
  84. r := []merkletree.KV{}
  85. lister := func(k []byte, v *merkletree.Node) (bool, error) {
  86. r = append(r, merkletree.KV{K: merkletree.Clone(k), V: *v})
  87. return true, nil
  88. }
  89. sto1 := sto.WithPrefix([]byte{1})
  90. err := sto1.Iterate(lister)
  91. assert.Nil(t, err)
  92. assert.Equal(t, 0, len(r))
  93. sto1tx, _ := sto1.NewTx()
  94. err = sto1tx.Put([]byte{1}, merkletree.NewNodeMiddle(&merkletree.Hash{4}, &merkletree.Hash{5}))
  95. assert.Nil(t, err)
  96. err = sto1tx.Put([]byte{2}, merkletree.NewNodeMiddle(&merkletree.Hash{5}, &merkletree.Hash{6}))
  97. assert.Nil(t, err)
  98. err = sto1tx.Put([]byte{3}, merkletree.NewNodeMiddle(&merkletree.Hash{6}, &merkletree.Hash{7}))
  99. assert.Nil(t, err)
  100. assert.Nil(t, sto1tx.Commit())
  101. sto2 := sto.WithPrefix([]byte{2})
  102. sto2tx, _ := sto2.NewTx()
  103. err = sto2tx.Put([]byte{1}, merkletree.NewNodeMiddle(&merkletree.Hash{7}, &merkletree.Hash{8}))
  104. assert.Nil(t, err)
  105. err = sto2tx.Put([]byte{2}, merkletree.NewNodeMiddle(&merkletree.Hash{8}, &merkletree.Hash{9}))
  106. assert.Nil(t, err)
  107. err = sto2tx.Put([]byte{3}, merkletree.NewNodeMiddle(&merkletree.Hash{9}, &merkletree.Hash{10}))
  108. assert.Nil(t, err)
  109. assert.Nil(t, sto2tx.Commit())
  110. r = []merkletree.KV{}
  111. err = sto1.Iterate(lister)
  112. assert.Nil(t, err)
  113. assert.Equal(t, 3, len(r))
  114. assert.Equal(t, merkletree.KV{K: []byte{1}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{4}, &merkletree.Hash{5})}, r[0])
  115. assert.Equal(t, merkletree.KV{K: []byte{2}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{5}, &merkletree.Hash{6})}, r[1])
  116. assert.Equal(t, merkletree.KV{K: []byte{3}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{6}, &merkletree.Hash{7})}, r[2])
  117. r = []merkletree.KV{}
  118. err = sto2.Iterate(lister)
  119. assert.Nil(t, err)
  120. assert.Equal(t, 3, len(r))
  121. assert.Equal(t, merkletree.KV{K: []byte{1}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{7}, &merkletree.Hash{8})}, r[0])
  122. assert.Equal(t, merkletree.KV{K: []byte{2}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{8}, &merkletree.Hash{9})}, r[1])
  123. assert.Equal(t, merkletree.KV{K: []byte{3}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{9}, &merkletree.Hash{10})}, r[2])
  124. }
  125. // TestConcatTx checks that the implementation of the db.Storage interface
  126. // behaves as expected
  127. func TestConcatTx(t *testing.T, sto merkletree.Storage) {
  128. k := []byte{9}
  129. sto1 := sto.WithPrefix([]byte{1})
  130. sto2 := sto.WithPrefix([]byte{2})
  131. // check within tx
  132. sto1tx, err := sto1.NewTx()
  133. if err != nil {
  134. panic(err)
  135. }
  136. err = sto1tx.Put(k, merkletree.NewNodeLeaf(&merkletree.Hash{4, 5, 6}, &merkletree.Hash{7, 8, 9}))
  137. assert.Nil(t, err)
  138. sto2tx, err := sto2.NewTx()
  139. if err != nil {
  140. panic(err)
  141. }
  142. err = sto2tx.Put(k, merkletree.NewNodeLeaf(&merkletree.Hash{8, 9}, &merkletree.Hash{10, 11}))
  143. assert.Nil(t, err)
  144. err = sto1tx.Add(sto2tx)
  145. assert.Nil(t, err)
  146. assert.Nil(t, sto1tx.Commit())
  147. // check outside tx
  148. v1, err := sto1.Get(k)
  149. assert.Nil(t, err)
  150. assert.Equal(t, v1, merkletree.NewNodeLeaf(&merkletree.Hash{4, 5, 6}, &merkletree.Hash{7, 8, 9}))
  151. v2, err := sto2.Get(k)
  152. assert.Nil(t, err)
  153. assert.Equal(t, v2, merkletree.NewNodeLeaf(&merkletree.Hash{8, 9}, &merkletree.Hash{10, 11}))
  154. }
  155. // TestList checks that the implementation of the db.Storage interface behaves
  156. // as expected
  157. func TestList(t *testing.T, sto merkletree.Storage) {
  158. sto1 := sto.WithPrefix([]byte{1})
  159. r1, err := sto1.List(100)
  160. assert.Nil(t, err)
  161. assert.Equal(t, 0, len(r1))
  162. sto1tx, _ := sto1.NewTx()
  163. err = sto1tx.Put([]byte{1}, merkletree.NewNodeMiddle(&merkletree.Hash{4}, &merkletree.Hash{5}))
  164. assert.Nil(t, err)
  165. err = sto1tx.Put([]byte{2}, merkletree.NewNodeMiddle(&merkletree.Hash{5}, &merkletree.Hash{6}))
  166. assert.Nil(t, err)
  167. err = sto1tx.Put([]byte{3}, merkletree.NewNodeMiddle(&merkletree.Hash{6}, &merkletree.Hash{7}))
  168. assert.Nil(t, err)
  169. assert.Nil(t, sto1tx.Commit())
  170. sto2 := sto.WithPrefix([]byte{2})
  171. sto2tx, _ := sto2.NewTx()
  172. err = sto2tx.Put([]byte{1}, merkletree.NewNodeMiddle(&merkletree.Hash{7}, &merkletree.Hash{8}))
  173. assert.Nil(t, err)
  174. err = sto2tx.Put([]byte{2}, merkletree.NewNodeMiddle(&merkletree.Hash{8}, &merkletree.Hash{9}))
  175. assert.Nil(t, err)
  176. err = sto2tx.Put([]byte{3}, merkletree.NewNodeMiddle(&merkletree.Hash{9}, &merkletree.Hash{10}))
  177. assert.Nil(t, err)
  178. assert.Nil(t, sto2tx.Commit())
  179. r, err := sto1.List(100)
  180. assert.Nil(t, err)
  181. assert.Equal(t, 3, len(r))
  182. assert.Equal(t, r[0], merkletree.KV{K: []byte{1}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{4}, &merkletree.Hash{5})})
  183. assert.Equal(t, r[1], merkletree.KV{K: []byte{2}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{5}, &merkletree.Hash{6})})
  184. assert.Equal(t, r[2], merkletree.KV{K: []byte{3}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{6}, &merkletree.Hash{7})})
  185. r, err = sto1.List(2)
  186. assert.Nil(t, err)
  187. assert.Equal(t, 2, len(r))
  188. assert.Equal(t, r[0], merkletree.KV{K: []byte{1}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{4}, &merkletree.Hash{5})})
  189. assert.Equal(t, r[1], merkletree.KV{K: []byte{2}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{5}, &merkletree.Hash{6})})
  190. }