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