meta_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. "math/bits"
  9. "sort"
  10. "testing"
  11. "github.com/syncthing/syncthing/lib/events"
  12. "github.com/syncthing/syncthing/lib/fs"
  13. "github.com/syncthing/syncthing/lib/protocol"
  14. )
  15. func TestEachFlagBit(t *testing.T) {
  16. cases := []struct {
  17. flags uint32
  18. iterations int
  19. }{
  20. {0, 0},
  21. {1<<0 | 1<<3, 2},
  22. {1 << 0, 1},
  23. {1 << 31, 1},
  24. {1<<10 | 1<<20 | 1<<30, 3},
  25. }
  26. for _, tc := range cases {
  27. var flags uint32
  28. iterations := 0
  29. eachFlagBit(tc.flags, func(f uint32) {
  30. iterations++
  31. flags |= f
  32. if bits.OnesCount32(f) != 1 {
  33. t.Error("expected exactly one bit to be set in every call")
  34. }
  35. })
  36. if flags != tc.flags {
  37. t.Errorf("expected 0x%x flags, got 0x%x", tc.flags, flags)
  38. }
  39. if iterations != tc.iterations {
  40. t.Errorf("expected %d iterations, got %d", tc.iterations, iterations)
  41. }
  42. }
  43. }
  44. func TestMetaDevices(t *testing.T) {
  45. d1 := protocol.DeviceID{1}
  46. d2 := protocol.DeviceID{2}
  47. meta := newMetadataTracker(nil, events.NoopLogger)
  48. meta.addFile(d1, protocol.FileInfo{Sequence: 1})
  49. meta.addFile(d1, protocol.FileInfo{Sequence: 2, LocalFlags: 1})
  50. meta.addFile(d2, protocol.FileInfo{Sequence: 1})
  51. meta.addFile(d2, protocol.FileInfo{Sequence: 2, LocalFlags: 2})
  52. meta.addFile(protocol.LocalDeviceID, protocol.FileInfo{Sequence: 1})
  53. // There are five device/flags combos
  54. if l := len(meta.counts.Counts); l < 5 {
  55. t.Error("expected at least five buckets, not", l)
  56. }
  57. // There are only two non-local devices
  58. devs := meta.devices()
  59. if l := len(devs); l != 2 {
  60. t.Fatal("expected two devices, not", l)
  61. }
  62. // Check that we got the two devices we expect
  63. sort.Slice(devs, func(a, b int) bool {
  64. return devs[a].Compare(devs[b]) == -1
  65. })
  66. if devs[0] != d1 {
  67. t.Error("first device should be d1")
  68. }
  69. if devs[1] != d2 {
  70. t.Error("second device should be d2")
  71. }
  72. }
  73. func TestMetaSequences(t *testing.T) {
  74. d1 := protocol.DeviceID{1}
  75. meta := newMetadataTracker(nil, events.NoopLogger)
  76. meta.addFile(d1, protocol.FileInfo{Sequence: 1})
  77. meta.addFile(d1, protocol.FileInfo{Sequence: 2, RawInvalid: true})
  78. meta.addFile(d1, protocol.FileInfo{Sequence: 3})
  79. meta.addFile(d1, protocol.FileInfo{Sequence: 4, RawInvalid: true})
  80. meta.addFile(protocol.LocalDeviceID, protocol.FileInfo{Sequence: 1})
  81. meta.addFile(protocol.LocalDeviceID, protocol.FileInfo{Sequence: 2})
  82. meta.addFile(protocol.LocalDeviceID, protocol.FileInfo{Sequence: 3, LocalFlags: 1})
  83. meta.addFile(protocol.LocalDeviceID, protocol.FileInfo{Sequence: 4, LocalFlags: 2})
  84. if seq := meta.Sequence(d1); seq != 4 {
  85. t.Error("sequence of first device should be 4, not", seq)
  86. }
  87. if seq := meta.Sequence(protocol.LocalDeviceID); seq != 4 {
  88. t.Error("sequence of first device should be 4, not", seq)
  89. }
  90. }
  91. func TestRecalcMeta(t *testing.T) {
  92. ldb := newLowlevelMemory(t)
  93. defer ldb.Close()
  94. // Add some files
  95. s1 := newFileSet(t, "test", fs.NewFilesystem(fs.FilesystemTypeFake, "fake"), ldb)
  96. files := []protocol.FileInfo{
  97. {Name: "a", Size: 1000},
  98. {Name: "b", Size: 2000},
  99. }
  100. s1.Update(protocol.LocalDeviceID, files)
  101. // Verify local/global size
  102. snap := s1.Snapshot()
  103. ls := snap.LocalSize()
  104. gs := snap.GlobalSize()
  105. snap.Release()
  106. if ls.Bytes != 3000 {
  107. t.Fatalf("Wrong initial local byte count, %d != 3000", ls.Bytes)
  108. }
  109. if gs.Bytes != 3000 {
  110. t.Fatalf("Wrong initial global byte count, %d != 3000", gs.Bytes)
  111. }
  112. // Reach into the database to make the metadata tracker intentionally
  113. // wrong and out of date
  114. curSeq := s1.meta.Sequence(protocol.LocalDeviceID)
  115. tran, err := ldb.newReadWriteTransaction()
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. s1.meta.mut.Lock()
  120. s1.meta.countsPtr(protocol.LocalDeviceID, 0).Sequence = curSeq - 1 // too low
  121. s1.meta.countsPtr(protocol.LocalDeviceID, 0).Bytes = 1234 // wrong
  122. s1.meta.countsPtr(protocol.GlobalDeviceID, 0).Bytes = 1234 // wrong
  123. s1.meta.dirty = true
  124. s1.meta.mut.Unlock()
  125. if err := s1.meta.toDB(tran, []byte("test")); err != nil {
  126. t.Fatal(err)
  127. }
  128. if err := tran.Commit(); err != nil {
  129. t.Fatal(err)
  130. }
  131. // Verify that our bad data "took"
  132. snap = s1.Snapshot()
  133. ls = snap.LocalSize()
  134. gs = snap.GlobalSize()
  135. snap.Release()
  136. if ls.Bytes != 1234 {
  137. t.Fatalf("Wrong changed local byte count, %d != 1234", ls.Bytes)
  138. }
  139. if gs.Bytes != 1234 {
  140. t.Fatalf("Wrong changed global byte count, %d != 1234", gs.Bytes)
  141. }
  142. // Create a new fileset, which will realize the inconsistency and recalculate
  143. s2 := newFileSet(t, "test", fs.NewFilesystem(fs.FilesystemTypeFake, "fake"), ldb)
  144. // Verify local/global size
  145. snap = s2.Snapshot()
  146. ls = snap.LocalSize()
  147. gs = snap.GlobalSize()
  148. snap.Release()
  149. if ls.Bytes != 3000 {
  150. t.Fatalf("Wrong fixed local byte count, %d != 3000", ls.Bytes)
  151. }
  152. if gs.Bytes != 3000 {
  153. t.Fatalf("Wrong fixed global byte count, %d != 3000", gs.Bytes)
  154. }
  155. }
  156. func TestMetaKeyCollisions(t *testing.T) {
  157. if protocol.LocalAllFlags&needFlag != 0 {
  158. t.Error("Collision between need flag and protocol local file flags")
  159. }
  160. }