util_test.go 3.0 KB

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