1
0

db_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package db
  7. import (
  8. "testing"
  9. "github.com/syncthing/syncthing/lib/db/backend"
  10. "github.com/syncthing/syncthing/lib/fs"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. )
  13. func genBlocks(n int) []protocol.BlockInfo {
  14. b := make([]protocol.BlockInfo, n)
  15. for i := range b {
  16. h := make([]byte, 32)
  17. for j := range h {
  18. h[j] = byte(i + j)
  19. }
  20. b[i].Size = int32(i)
  21. b[i].Hash = h
  22. }
  23. return b
  24. }
  25. func TestIgnoredFiles(t *testing.T) {
  26. ldb, err := openJSONS("testdata/v0.14.48-ignoredfiles.db.jsons")
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. db := NewLowlevel(ldb)
  31. if err := UpdateSchema(db); err != nil {
  32. t.Fatal(err)
  33. }
  34. fs := NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), db)
  35. // The contents of the database are like this:
  36. //
  37. // fs := NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), db)
  38. // fs.Update(protocol.LocalDeviceID, []protocol.FileInfo{
  39. // { // invalid (ignored) file
  40. // Name: "foo",
  41. // Type: protocol.FileInfoTypeFile,
  42. // Invalid: true,
  43. // Version: protocol.Vector{Counters: []protocol.Counter{{ID: 1, Value: 1000}}},
  44. // },
  45. // { // regular file
  46. // Name: "bar",
  47. // Type: protocol.FileInfoTypeFile,
  48. // Version: protocol.Vector{Counters: []protocol.Counter{{ID: 1, Value: 1001}}},
  49. // },
  50. // })
  51. // fs.Update(protocol.DeviceID{42}, []protocol.FileInfo{
  52. // { // invalid file
  53. // Name: "baz",
  54. // Type: protocol.FileInfoTypeFile,
  55. // Invalid: true,
  56. // Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 1000}}},
  57. // },
  58. // { // regular file
  59. // Name: "quux",
  60. // Type: protocol.FileInfoTypeFile,
  61. // Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 1002}}},
  62. // },
  63. // })
  64. // Local files should have the "ignored" bit in addition to just being
  65. // generally invalid if we want to look at the simulation of that bit.
  66. snap := fs.Snapshot()
  67. defer snap.Release()
  68. fi, ok := snap.Get(protocol.LocalDeviceID, "foo")
  69. if !ok {
  70. t.Fatal("foo should exist")
  71. }
  72. if !fi.IsInvalid() {
  73. t.Error("foo should be invalid")
  74. }
  75. if !fi.IsIgnored() {
  76. t.Error("foo should be ignored")
  77. }
  78. fi, ok = snap.Get(protocol.LocalDeviceID, "bar")
  79. if !ok {
  80. t.Fatal("bar should exist")
  81. }
  82. if fi.IsInvalid() {
  83. t.Error("bar should not be invalid")
  84. }
  85. if fi.IsIgnored() {
  86. t.Error("bar should not be ignored")
  87. }
  88. // Remote files have the invalid bit as usual, and the IsInvalid() method
  89. // should pick this up too.
  90. fi, ok = snap.Get(protocol.DeviceID{42}, "baz")
  91. if !ok {
  92. t.Fatal("baz should exist")
  93. }
  94. if !fi.IsInvalid() {
  95. t.Error("baz should be invalid")
  96. }
  97. if !fi.IsInvalid() {
  98. t.Error("baz should be invalid")
  99. }
  100. fi, ok = snap.Get(protocol.DeviceID{42}, "quux")
  101. if !ok {
  102. t.Fatal("quux should exist")
  103. }
  104. if fi.IsInvalid() {
  105. t.Error("quux should not be invalid")
  106. }
  107. if fi.IsInvalid() {
  108. t.Error("quux should not be invalid")
  109. }
  110. }
  111. const myID = 1
  112. var (
  113. remoteDevice0, remoteDevice1 protocol.DeviceID
  114. update0to3Folder = "UpdateSchema0to3"
  115. invalid = "invalid"
  116. slashPrefixed = "/notgood"
  117. haveUpdate0to3 map[protocol.DeviceID]fileList
  118. )
  119. func init() {
  120. remoteDevice0, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  121. remoteDevice1, _ = protocol.DeviceIDFromString("I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU")
  122. haveUpdate0to3 = map[protocol.DeviceID]fileList{
  123. protocol.LocalDeviceID: {
  124. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  125. protocol.FileInfo{Name: slashPrefixed, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  126. },
  127. remoteDevice0: {
  128. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  129. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  130. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  131. },
  132. remoteDevice1: {
  133. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  134. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  135. protocol.FileInfo{Name: invalid, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  136. },
  137. }
  138. }
  139. func TestUpdate0to3(t *testing.T) {
  140. ldb, err := openJSONS("testdata/v0.14.45-update0to3.db.jsons")
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. db := NewLowlevel(ldb)
  145. updater := schemaUpdater{db}
  146. folder := []byte(update0to3Folder)
  147. if err := updater.updateSchema0to1(0); err != nil {
  148. t.Fatal(err)
  149. }
  150. trans, err := db.newReadOnlyTransaction()
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. if _, ok, err := trans.getFile(folder, protocol.LocalDeviceID[:], []byte(slashPrefixed)); err != nil {
  155. t.Fatal(err)
  156. } else if ok {
  157. t.Error("File prefixed by '/' was not removed during transition to schema 1")
  158. }
  159. key, err := db.keyer.GenerateGlobalVersionKey(nil, folder, []byte(invalid))
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. if _, err := db.Get(key); err != nil {
  164. t.Error("Invalid file wasn't added to global list")
  165. }
  166. if err := updater.updateSchema1to2(1); err != nil {
  167. t.Fatal(err)
  168. }
  169. found := false
  170. trans, err = db.newReadOnlyTransaction()
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. _ = trans.withHaveSequence(folder, 0, func(fi FileIntf) bool {
  175. f := fi.(protocol.FileInfo)
  176. l.Infoln(f)
  177. if found {
  178. t.Error("Unexpected additional file via sequence", f.FileName())
  179. return true
  180. }
  181. if e := haveUpdate0to3[protocol.LocalDeviceID][0]; f.IsEquivalentOptional(e, 0, true, true, 0) {
  182. found = true
  183. } else {
  184. t.Errorf("Wrong file via sequence, got %v, expected %v", f, e)
  185. }
  186. return true
  187. })
  188. if !found {
  189. t.Error("Local file wasn't added to sequence bucket", err)
  190. }
  191. if err := updater.updateSchema2to3(2); err != nil {
  192. t.Fatal(err)
  193. }
  194. need := map[string]protocol.FileInfo{
  195. haveUpdate0to3[remoteDevice0][0].Name: haveUpdate0to3[remoteDevice0][0],
  196. haveUpdate0to3[remoteDevice1][0].Name: haveUpdate0to3[remoteDevice1][0],
  197. haveUpdate0to3[remoteDevice0][2].Name: haveUpdate0to3[remoteDevice0][2],
  198. }
  199. trans, err = db.newReadOnlyTransaction()
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. _ = trans.withNeed(folder, protocol.LocalDeviceID[:], false, func(fi FileIntf) bool {
  204. e, ok := need[fi.FileName()]
  205. if !ok {
  206. t.Error("Got unexpected needed file:", fi.FileName())
  207. }
  208. f := fi.(protocol.FileInfo)
  209. delete(need, f.Name)
  210. if !f.IsEquivalentOptional(e, 0, true, true, 0) {
  211. t.Errorf("Wrong needed file, got %v, expected %v", f, e)
  212. }
  213. return true
  214. })
  215. for n := range need {
  216. t.Errorf(`Missing needed file "%v"`, n)
  217. }
  218. }
  219. func TestDowngrade(t *testing.T) {
  220. db := NewLowlevel(backend.OpenMemory())
  221. // sets the min version etc
  222. if err := UpdateSchema(db); err != nil {
  223. t.Fatal(err)
  224. }
  225. // Bump the database version to something newer than we actually support
  226. miscDB := NewMiscDataNamespace(db)
  227. if err := miscDB.PutInt64("dbVersion", dbVersion+1); err != nil {
  228. t.Fatal(err)
  229. }
  230. l.Infoln(dbVersion)
  231. // Pretend we just opened the DB and attempt to update it again
  232. err := UpdateSchema(db)
  233. if err, ok := err.(databaseDowngradeError); !ok {
  234. t.Fatal("Expected error due to database downgrade, got", err)
  235. } else if err.minSyncthingVersion != dbMinSyncthingVersion {
  236. t.Fatalf("Error has %v as min Syncthing version, expected %v", err.minSyncthingVersion, dbMinSyncthingVersion)
  237. }
  238. }