util_test.go 3.2 KB

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