util_test.go 4.9 KB

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