util_test.go 4.4 KB

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