blockmap_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package files
  16. import (
  17. "testing"
  18. "github.com/syncthing/syncthing/internal/config"
  19. "github.com/syncthing/syncthing/internal/protocol"
  20. "github.com/syndtr/goleveldb/leveldb"
  21. "github.com/syndtr/goleveldb/leveldb/storage"
  22. )
  23. func genBlocks(n int) []protocol.BlockInfo {
  24. b := make([]protocol.BlockInfo, n)
  25. for i := range b {
  26. h := make([]byte, 32)
  27. for j := range h {
  28. h[j] = byte(i + j)
  29. }
  30. b[i].Size = uint32(i)
  31. b[i].Hash = h
  32. }
  33. return b
  34. }
  35. var f1, f2, f3 protocol.FileInfo
  36. func init() {
  37. blocks := genBlocks(30)
  38. f1 = protocol.FileInfo{
  39. Name: "f1",
  40. Blocks: blocks[:10],
  41. }
  42. f2 = protocol.FileInfo{
  43. Name: "f2",
  44. Blocks: blocks[10:20],
  45. }
  46. f3 = protocol.FileInfo{
  47. Name: "f3",
  48. Blocks: blocks[20:],
  49. }
  50. }
  51. func setup() (*leveldb.DB, *BlockFinder) {
  52. // Setup
  53. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  54. if err != nil {
  55. panic(err)
  56. }
  57. wrapper := config.Wrap("", config.Configuration{})
  58. wrapper.SetFolder(config.FolderConfiguration{
  59. ID: "folder1",
  60. })
  61. wrapper.SetFolder(config.FolderConfiguration{
  62. ID: "folder2",
  63. })
  64. return db, NewBlockFinder(db, wrapper)
  65. }
  66. func dbEmpty(db *leveldb.DB) bool {
  67. iter := db.NewIterator(nil, nil)
  68. defer iter.Release()
  69. if iter.Next() {
  70. return false
  71. }
  72. return true
  73. }
  74. func TestBlockMapAddUpdateWipe(t *testing.T) {
  75. db, f := setup()
  76. if !dbEmpty(db) {
  77. t.Fatal("db not empty")
  78. }
  79. m := NewBlockMap(db, "folder1")
  80. f3.Flags |= protocol.FlagDirectory
  81. err := m.Add([]protocol.FileInfo{f1, f2, f3})
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. f.Iterate(f1.Blocks[0].Hash, func(folder, file string, index uint32) bool {
  86. if folder != "folder1" || file != "f1" || index != 0 {
  87. t.Fatal("Mismatch")
  88. }
  89. return true
  90. })
  91. f.Iterate(f2.Blocks[0].Hash, func(folder, file string, index uint32) bool {
  92. if folder != "folder1" || file != "f2" || index != 0 {
  93. t.Fatal("Mismatch")
  94. }
  95. return true
  96. })
  97. f.Iterate(f3.Blocks[0].Hash, func(folder, file string, index uint32) bool {
  98. t.Fatal("Unexpected block")
  99. return true
  100. })
  101. f3.Flags = f1.Flags
  102. f1.Flags |= protocol.FlagDeleted
  103. f2.Flags |= protocol.FlagInvalid
  104. // Should remove
  105. err = m.Update([]protocol.FileInfo{f1, f2, f3})
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. f.Iterate(f1.Blocks[0].Hash, func(folder, file string, index uint32) bool {
  110. t.Fatal("Unexpected block")
  111. return false
  112. })
  113. f.Iterate(f2.Blocks[0].Hash, func(folder, file string, index uint32) bool {
  114. t.Fatal("Unexpected block")
  115. return false
  116. })
  117. f.Iterate(f3.Blocks[0].Hash, func(folder, file string, index uint32) bool {
  118. if folder != "folder1" || file != "f3" || index != 0 {
  119. t.Fatal("Mismatch")
  120. }
  121. return true
  122. })
  123. err = m.Drop()
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. if !dbEmpty(db) {
  128. t.Fatal("db not empty")
  129. }
  130. // Should not add
  131. err = m.Add([]protocol.FileInfo{f1, f2})
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. if !dbEmpty(db) {
  136. t.Fatal("db not empty")
  137. }
  138. f1.Flags = 0
  139. f2.Flags = 0
  140. f3.Flags = 0
  141. }
  142. func TestBlockMapFinderLookup(t *testing.T) {
  143. db, f := setup()
  144. m1 := NewBlockMap(db, "folder1")
  145. m2 := NewBlockMap(db, "folder2")
  146. err := m1.Add([]protocol.FileInfo{f1})
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. err = m2.Add([]protocol.FileInfo{f1})
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. counter := 0
  155. f.Iterate(f1.Blocks[0].Hash, func(folder, file string, index uint32) bool {
  156. counter++
  157. switch counter {
  158. case 1:
  159. if folder != "folder1" || file != "f1" || index != 0 {
  160. t.Fatal("Mismatch")
  161. }
  162. case 2:
  163. if folder != "folder2" || file != "f1" || index != 0 {
  164. t.Fatal("Mismatch")
  165. }
  166. default:
  167. t.Fatal("Unexpected block")
  168. }
  169. return false
  170. })
  171. if counter != 2 {
  172. t.Fatal("Incorrect count", counter)
  173. }
  174. f1.Flags |= protocol.FlagDeleted
  175. err = m1.Update([]protocol.FileInfo{f1})
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. counter = 0
  180. f.Iterate(f1.Blocks[0].Hash, func(folder, file string, index uint32) bool {
  181. counter++
  182. switch counter {
  183. case 1:
  184. if folder != "folder2" || file != "f1" || index != 0 {
  185. t.Fatal("Mismatch")
  186. }
  187. default:
  188. t.Fatal("Unexpected block")
  189. }
  190. return false
  191. })
  192. if counter != 1 {
  193. t.Fatal("Incorrect count")
  194. }
  195. }