util_test.go 3.1 KB

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