util_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (C) 2018 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. "encoding/json"
  9. "io"
  10. "os"
  11. "testing"
  12. "github.com/syncthing/syncthing/lib/db/backend"
  13. "github.com/syncthing/syncthing/lib/events"
  14. )
  15. // writeJSONS serializes the database to a JSON stream that can be checked
  16. // in to the repo and used for tests.
  17. func writeJSONS(w io.Writer, db backend.Backend) {
  18. it, err := db.NewPrefixIterator(nil)
  19. if err != nil {
  20. panic(err)
  21. }
  22. defer it.Release()
  23. enc := json.NewEncoder(w)
  24. for it.Next() {
  25. err := enc.Encode(map[string][]byte{
  26. "k": it.Key(),
  27. "v": it.Value(),
  28. })
  29. if err != nil {
  30. panic(err)
  31. }
  32. }
  33. }
  34. // we know this function isn't generally used, nonetheless we want it in
  35. // here and the linter to not complain.
  36. var _ = writeJSONS
  37. // openJSONS reads a JSON stream file into a backend DB
  38. func openJSONS(file string) (backend.Backend, error) {
  39. fd, err := os.Open(file)
  40. if err != nil {
  41. return nil, err
  42. }
  43. dec := json.NewDecoder(fd)
  44. db := backend.OpenMemory()
  45. for {
  46. var row map[string][]byte
  47. err := dec.Decode(&row)
  48. if err == io.EOF {
  49. break
  50. } else if err != nil {
  51. return nil, err
  52. }
  53. if err := db.Put(row["k"], row["v"]); err != nil {
  54. return nil, err
  55. }
  56. }
  57. return db, nil
  58. }
  59. func newLowlevel(t testing.TB, backend backend.Backend) *Lowlevel {
  60. t.Helper()
  61. ll, err := NewLowlevel(backend, events.NoopLogger)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. return ll
  66. }
  67. func newLowlevelMemory(t testing.TB) *Lowlevel {
  68. return newLowlevel(t, backend.OpenMemory())
  69. }
  70. func newFileSet(t testing.TB, folder string, db *Lowlevel) *FileSet {
  71. t.Helper()
  72. fset, err := NewFileSet(folder, db)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. return fset
  77. }
  78. func snapshot(t testing.TB, fset *FileSet) *Snapshot {
  79. t.Helper()
  80. snap, err := fset.Snapshot()
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. return snap
  85. }
  86. // The following commented tests were used to generate jsons files to stdout for
  87. // future tests and are kept here for reference (reuse).
  88. // TestGenerateIgnoredFilesDB generates a database with files with invalid flags,
  89. // local and remote, in the format used in 0.14.48.
  90. // func TestGenerateIgnoredFilesDB(t *testing.T) {
  91. // db := OpenMemory()
  92. // fs := newFileSet(t, "test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), db)
  93. // fs.Update(protocol.LocalDeviceID, []protocol.FileInfo{
  94. // { // invalid (ignored) file
  95. // Name: "foo",
  96. // Type: protocol.FileInfoTypeFile,
  97. // Invalid: true,
  98. // Version: protocol.Vector{Counters: []protocol.Counter{{ID: 1, Value: 1000}}},
  99. // },
  100. // { // regular file
  101. // Name: "bar",
  102. // Type: protocol.FileInfoTypeFile,
  103. // Version: protocol.Vector{Counters: []protocol.Counter{{ID: 1, Value: 1001}}},
  104. // },
  105. // })
  106. // fs.Update(protocol.DeviceID{42}, []protocol.FileInfo{
  107. // { // invalid file
  108. // Name: "baz",
  109. // Type: protocol.FileInfoTypeFile,
  110. // Invalid: true,
  111. // Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 1000}}},
  112. // },
  113. // { // regular file
  114. // Name: "quux",
  115. // Type: protocol.FileInfoTypeFile,
  116. // Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 1002}}},
  117. // },
  118. // })
  119. // writeJSONS(os.Stdout, db.DB)
  120. // }
  121. // TestGenerateUpdate0to3DB generates a database with files with invalid flags, prefixed
  122. // by a slash and other files to test database migration from version 0 to 3, in the
  123. // format used in 0.14.45.
  124. // func TestGenerateUpdate0to3DB(t *testing.T) {
  125. // db := OpenMemory()
  126. // fs := newFileSet(t, update0to3Folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), db)
  127. // for devID, files := range haveUpdate0to3 {
  128. // fs.Update(devID, files)
  129. // }
  130. // writeJSONS(os.Stdout, db.DB)
  131. // }
  132. // func TestGenerateUpdateTo10(t *testing.T) {
  133. // db := newLowlevelMemory(t)
  134. // defer db.Close()
  135. // if err := UpdateSchema(db); err != nil {
  136. // t.Fatal(err)
  137. // }
  138. // fs := newFileSet(t, "test", fs.NewFilesystem(fs.FilesystemTypeFake, ""), db)
  139. // files := []protocol.FileInfo{
  140. // {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Deleted: true, Sequence: 1},
  141. // {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2), Sequence: 2},
  142. // {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Deleted: true, Sequence: 3},
  143. // }
  144. // fs.Update(protocol.LocalDeviceID, files)
  145. // files[1].Version = files[1].Version.Update(remoteDevice0.Short())
  146. // files[1].Deleted = true
  147. // files[2].Version = files[2].Version.Update(remoteDevice0.Short())
  148. // files[2].Blocks = genBlocks(1)
  149. // files[2].Deleted = false
  150. // fs.Update(remoteDevice0, files)
  151. // fd, err := os.Create("./testdata/v1.4.0-updateTo10.json")
  152. // if err != nil {
  153. // panic(err)
  154. // }
  155. // defer fd.Close()
  156. // writeJSONS(fd, db)
  157. // }