set_test.go 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. // Copyright (C) 2014 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_test
  7. import (
  8. "bytes"
  9. "fmt"
  10. "os"
  11. "path/filepath"
  12. "sort"
  13. "testing"
  14. "time"
  15. "github.com/d4l3k/messagediff"
  16. "github.com/syncthing/syncthing/lib/db"
  17. "github.com/syncthing/syncthing/lib/db/backend"
  18. "github.com/syncthing/syncthing/lib/fs"
  19. "github.com/syncthing/syncthing/lib/protocol"
  20. )
  21. var remoteDevice0, remoteDevice1 protocol.DeviceID
  22. func init() {
  23. remoteDevice0, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  24. remoteDevice1, _ = protocol.DeviceIDFromString("I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU")
  25. }
  26. const myID = 1
  27. func genBlocks(n int) []protocol.BlockInfo {
  28. b := make([]protocol.BlockInfo, n)
  29. for i := range b {
  30. h := make([]byte, 32)
  31. for j := range h {
  32. h[j] = byte(i + j)
  33. }
  34. b[i].Size = int32(i)
  35. b[i].Hash = h
  36. }
  37. return b
  38. }
  39. func globalList(s *db.FileSet) []protocol.FileInfo {
  40. var fs []protocol.FileInfo
  41. snap := s.Snapshot()
  42. defer snap.Release()
  43. snap.WithGlobal(func(fi db.FileIntf) bool {
  44. f := fi.(protocol.FileInfo)
  45. fs = append(fs, f)
  46. return true
  47. })
  48. return fs
  49. }
  50. func globalListPrefixed(s *db.FileSet, prefix string) []db.FileInfoTruncated {
  51. var fs []db.FileInfoTruncated
  52. snap := s.Snapshot()
  53. defer snap.Release()
  54. snap.WithPrefixedGlobalTruncated(prefix, func(fi db.FileIntf) bool {
  55. f := fi.(db.FileInfoTruncated)
  56. fs = append(fs, f)
  57. return true
  58. })
  59. return fs
  60. }
  61. func haveList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  62. var fs []protocol.FileInfo
  63. snap := s.Snapshot()
  64. defer snap.Release()
  65. snap.WithHave(n, func(fi db.FileIntf) bool {
  66. f := fi.(protocol.FileInfo)
  67. fs = append(fs, f)
  68. return true
  69. })
  70. return fs
  71. }
  72. func haveListPrefixed(s *db.FileSet, n protocol.DeviceID, prefix string) []db.FileInfoTruncated {
  73. var fs []db.FileInfoTruncated
  74. snap := s.Snapshot()
  75. defer snap.Release()
  76. snap.WithPrefixedHaveTruncated(n, prefix, func(fi db.FileIntf) bool {
  77. f := fi.(db.FileInfoTruncated)
  78. fs = append(fs, f)
  79. return true
  80. })
  81. return fs
  82. }
  83. func needList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  84. var fs []protocol.FileInfo
  85. snap := s.Snapshot()
  86. defer snap.Release()
  87. snap.WithNeed(n, func(fi db.FileIntf) bool {
  88. f := fi.(protocol.FileInfo)
  89. fs = append(fs, f)
  90. return true
  91. })
  92. return fs
  93. }
  94. type fileList []protocol.FileInfo
  95. func (l fileList) Len() int {
  96. return len(l)
  97. }
  98. func (l fileList) Less(a, b int) bool {
  99. return l[a].Name < l[b].Name
  100. }
  101. func (l fileList) Swap(a, b int) {
  102. l[a], l[b] = l[b], l[a]
  103. }
  104. func (l fileList) String() string {
  105. var b bytes.Buffer
  106. b.WriteString("[]protocol.FileList{\n")
  107. for _, f := range l {
  108. fmt.Fprintf(&b, " %q: #%v, %d bytes, %d blocks, perms=%o\n", f.Name, f.Version, f.Size, len(f.Blocks), f.Permissions)
  109. }
  110. b.WriteString("}")
  111. return b.String()
  112. }
  113. func TestGlobalSet(t *testing.T) {
  114. ldb := db.NewLowlevel(backend.OpenMemory())
  115. defer ldb.Close()
  116. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  117. local0 := fileList{
  118. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  119. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  120. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  121. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  122. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  123. }
  124. local1 := fileList{
  125. protocol.FileInfo{Name: "a", Sequence: 6, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  126. protocol.FileInfo{Name: "b", Sequence: 7, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  127. protocol.FileInfo{Name: "c", Sequence: 8, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  128. protocol.FileInfo{Name: "d", Sequence: 9, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  129. protocol.FileInfo{Name: "z", Sequence: 10, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  130. }
  131. localTot := fileList{
  132. local1[0],
  133. local1[1],
  134. local1[2],
  135. local1[3],
  136. protocol.FileInfo{Name: "z", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  137. }
  138. remote0 := fileList{
  139. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  140. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  141. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  142. }
  143. remote1 := fileList{
  144. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(6)},
  145. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(7)},
  146. }
  147. remoteTot := fileList{
  148. remote0[0],
  149. remote1[0],
  150. remote0[2],
  151. remote1[1],
  152. }
  153. expectedGlobal := fileList{
  154. remote0[0], // a
  155. remote1[0], // b
  156. remote0[2], // c
  157. localTot[3], // d
  158. remote1[1], // e
  159. localTot[4], // z
  160. }
  161. expectedLocalNeed := fileList{
  162. remote1[0],
  163. remote0[2],
  164. remote1[1],
  165. }
  166. expectedRemoteNeed := fileList{
  167. local0[3],
  168. }
  169. replace(m, protocol.LocalDeviceID, local0)
  170. replace(m, protocol.LocalDeviceID, local1)
  171. replace(m, remoteDevice0, remote0)
  172. m.Update(remoteDevice0, remote1)
  173. g := fileList(globalList(m))
  174. sort.Sort(g)
  175. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
  176. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal)
  177. }
  178. globalFiles, globalDirectories, globalDeleted, globalBytes := int32(0), int32(0), int32(0), int64(0)
  179. for _, f := range g {
  180. if f.IsInvalid() {
  181. continue
  182. }
  183. switch {
  184. case f.IsDeleted():
  185. globalDeleted++
  186. case f.IsDirectory():
  187. globalDirectories++
  188. default:
  189. globalFiles++
  190. }
  191. globalBytes += f.FileSize()
  192. }
  193. gs := globalSize(m)
  194. if gs.Files != globalFiles {
  195. t.Errorf("Incorrect GlobalSize files; %d != %d", gs.Files, globalFiles)
  196. }
  197. if gs.Directories != globalDirectories {
  198. t.Errorf("Incorrect GlobalSize directories; %d != %d", gs.Directories, globalDirectories)
  199. }
  200. if gs.Deleted != globalDeleted {
  201. t.Errorf("Incorrect GlobalSize deleted; %d != %d", gs.Deleted, globalDeleted)
  202. }
  203. if gs.Bytes != globalBytes {
  204. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  205. }
  206. h := fileList(haveList(m, protocol.LocalDeviceID))
  207. sort.Sort(h)
  208. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  209. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot)
  210. }
  211. haveFiles, haveDirectories, haveDeleted, haveBytes := int32(0), int32(0), int32(0), int64(0)
  212. for _, f := range h {
  213. if f.IsInvalid() {
  214. continue
  215. }
  216. switch {
  217. case f.IsDeleted():
  218. haveDeleted++
  219. case f.IsDirectory():
  220. haveDirectories++
  221. default:
  222. haveFiles++
  223. }
  224. haveBytes += f.FileSize()
  225. }
  226. ls := localSize(m)
  227. if ls.Files != haveFiles {
  228. t.Errorf("Incorrect LocalSize files; %d != %d", ls.Files, haveFiles)
  229. }
  230. if ls.Directories != haveDirectories {
  231. t.Errorf("Incorrect LocalSize directories; %d != %d", ls.Directories, haveDirectories)
  232. }
  233. if ls.Deleted != haveDeleted {
  234. t.Errorf("Incorrect LocalSize deleted; %d != %d", ls.Deleted, haveDeleted)
  235. }
  236. if ls.Bytes != haveBytes {
  237. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  238. }
  239. h = fileList(haveList(m, remoteDevice0))
  240. sort.Sort(h)
  241. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  242. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, remoteTot)
  243. }
  244. n := fileList(needList(m, protocol.LocalDeviceID))
  245. sort.Sort(n)
  246. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  247. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedLocalNeed)
  248. }
  249. n = fileList(needList(m, remoteDevice0))
  250. sort.Sort(n)
  251. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  252. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  253. }
  254. snap := m.Snapshot()
  255. defer snap.Release()
  256. f, ok := snap.Get(protocol.LocalDeviceID, "b")
  257. if !ok {
  258. t.Error("Unexpectedly not OK")
  259. }
  260. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  261. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  262. }
  263. f, ok = snap.Get(remoteDevice0, "b")
  264. if !ok {
  265. t.Error("Unexpectedly not OK")
  266. }
  267. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  268. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  269. }
  270. f, ok = snap.GetGlobal("b")
  271. if !ok {
  272. t.Error("Unexpectedly not OK")
  273. }
  274. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  275. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  276. }
  277. f, ok = snap.Get(protocol.LocalDeviceID, "zz")
  278. if ok {
  279. t.Error("Unexpectedly OK")
  280. }
  281. if f.Name != "" {
  282. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  283. }
  284. f, ok = snap.GetGlobal("zz")
  285. if ok {
  286. t.Error("Unexpectedly OK")
  287. }
  288. if f.Name != "" {
  289. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  290. }
  291. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  292. a := snap.Availability("a")
  293. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  294. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  295. }
  296. a = snap.Availability("b")
  297. if len(a) != 1 || a[0] != remoteDevice0 {
  298. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  299. }
  300. a = snap.Availability("d")
  301. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  302. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  303. }
  304. }
  305. func TestNeedWithInvalid(t *testing.T) {
  306. ldb := db.NewLowlevel(backend.OpenMemory())
  307. defer ldb.Close()
  308. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  309. localHave := fileList{
  310. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  311. }
  312. remote0Have := fileList{
  313. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  314. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  315. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  316. }
  317. remote1Have := fileList{
  318. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  319. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  320. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  321. }
  322. expectedNeed := fileList{
  323. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  324. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  325. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  326. }
  327. replace(s, protocol.LocalDeviceID, localHave)
  328. replace(s, remoteDevice0, remote0Have)
  329. replace(s, remoteDevice1, remote1Have)
  330. need := fileList(needList(s, protocol.LocalDeviceID))
  331. sort.Sort(need)
  332. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  333. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  334. }
  335. }
  336. func TestUpdateToInvalid(t *testing.T) {
  337. ldb := db.NewLowlevel(backend.OpenMemory())
  338. defer ldb.Close()
  339. folder := "test"
  340. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  341. f := db.NewBlockFinder(ldb)
  342. localHave := fileList{
  343. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  344. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  345. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), LocalFlags: protocol.FlagLocalIgnored},
  346. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  347. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, LocalFlags: protocol.FlagLocalIgnored},
  348. }
  349. replace(s, protocol.LocalDeviceID, localHave)
  350. have := fileList(haveList(s, protocol.LocalDeviceID))
  351. sort.Sort(have)
  352. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  353. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  354. }
  355. oldBlockHash := localHave[1].Blocks[0].Hash
  356. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  357. localHave[1].Blocks = nil
  358. localHave[4].LocalFlags = 0
  359. localHave[4].Blocks = genBlocks(3)
  360. s.Update(protocol.LocalDeviceID, append(fileList{}, localHave[1], localHave[4]))
  361. have = fileList(haveList(s, protocol.LocalDeviceID))
  362. sort.Sort(have)
  363. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  364. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  365. }
  366. f.Iterate([]string{folder}, oldBlockHash, func(folder, file string, index int32) bool {
  367. if file == localHave[1].Name {
  368. t.Errorf("Found unexpected block in blockmap for invalidated file")
  369. return true
  370. }
  371. return false
  372. })
  373. if !f.Iterate([]string{folder}, localHave[4].Blocks[0].Hash, func(folder, file string, index int32) bool {
  374. return file == localHave[4].Name
  375. }) {
  376. t.Errorf("First block of un-invalidated file is missing from blockmap")
  377. }
  378. }
  379. func TestInvalidAvailability(t *testing.T) {
  380. ldb := db.NewLowlevel(backend.OpenMemory())
  381. defer ldb.Close()
  382. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  383. remote0Have := fileList{
  384. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  385. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  386. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  387. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  388. }
  389. remote1Have := fileList{
  390. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  391. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  392. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  393. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  394. }
  395. replace(s, remoteDevice0, remote0Have)
  396. replace(s, remoteDevice1, remote1Have)
  397. snap := s.Snapshot()
  398. defer snap.Release()
  399. if av := snap.Availability("both"); len(av) != 2 {
  400. t.Error("Incorrect availability for 'both':", av)
  401. }
  402. if av := snap.Availability("r0only"); len(av) != 1 || av[0] != remoteDevice0 {
  403. t.Error("Incorrect availability for 'r0only':", av)
  404. }
  405. if av := snap.Availability("r1only"); len(av) != 1 || av[0] != remoteDevice1 {
  406. t.Error("Incorrect availability for 'r1only':", av)
  407. }
  408. if av := snap.Availability("none"); len(av) != 0 {
  409. t.Error("Incorrect availability for 'none':", av)
  410. }
  411. }
  412. func TestGlobalReset(t *testing.T) {
  413. ldb := db.NewLowlevel(backend.OpenMemory())
  414. defer ldb.Close()
  415. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  416. local := []protocol.FileInfo{
  417. {Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  418. {Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  419. {Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  420. {Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  421. }
  422. remote := []protocol.FileInfo{
  423. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  424. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  425. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  426. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  427. }
  428. replace(m, protocol.LocalDeviceID, local)
  429. g := globalList(m)
  430. sort.Sort(fileList(g))
  431. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  432. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  433. }
  434. replace(m, remoteDevice0, remote)
  435. replace(m, remoteDevice0, nil)
  436. g = globalList(m)
  437. sort.Sort(fileList(g))
  438. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  439. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  440. }
  441. }
  442. func TestNeed(t *testing.T) {
  443. ldb := db.NewLowlevel(backend.OpenMemory())
  444. defer ldb.Close()
  445. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  446. local := []protocol.FileInfo{
  447. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  448. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  449. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  450. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  451. }
  452. remote := []protocol.FileInfo{
  453. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  454. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  455. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  456. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  457. }
  458. shouldNeed := []protocol.FileInfo{
  459. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  460. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  461. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  462. }
  463. replace(m, protocol.LocalDeviceID, local)
  464. replace(m, remoteDevice0, remote)
  465. need := needList(m, protocol.LocalDeviceID)
  466. sort.Sort(fileList(need))
  467. sort.Sort(fileList(shouldNeed))
  468. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  469. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  470. }
  471. }
  472. func TestSequence(t *testing.T) {
  473. ldb := db.NewLowlevel(backend.OpenMemory())
  474. defer ldb.Close()
  475. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  476. local1 := []protocol.FileInfo{
  477. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  478. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  479. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  480. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  481. }
  482. local2 := []protocol.FileInfo{
  483. local1[0],
  484. // [1] deleted
  485. local1[2],
  486. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  487. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  488. }
  489. replace(m, protocol.LocalDeviceID, local1)
  490. c0 := m.Sequence(protocol.LocalDeviceID)
  491. replace(m, protocol.LocalDeviceID, local2)
  492. c1 := m.Sequence(protocol.LocalDeviceID)
  493. if !(c1 > c0) {
  494. t.Fatal("Local version number should have incremented")
  495. }
  496. }
  497. func TestListDropFolder(t *testing.T) {
  498. ldb := db.NewLowlevel(backend.OpenMemory())
  499. defer ldb.Close()
  500. s0 := db.NewFileSet("test0", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  501. local1 := []protocol.FileInfo{
  502. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  503. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  504. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  505. }
  506. replace(s0, protocol.LocalDeviceID, local1)
  507. s1 := db.NewFileSet("test1", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  508. local2 := []protocol.FileInfo{
  509. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  510. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  511. {Name: "f", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  512. }
  513. replace(s1, remoteDevice0, local2)
  514. // Check that we have both folders and their data is in the global list
  515. expectedFolderList := []string{"test0", "test1"}
  516. actualFolderList := ldb.ListFolders()
  517. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  518. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  519. }
  520. if l := len(globalList(s0)); l != 3 {
  521. t.Errorf("Incorrect global length %d != 3 for s0", l)
  522. }
  523. if l := len(globalList(s1)); l != 3 {
  524. t.Errorf("Incorrect global length %d != 3 for s1", l)
  525. }
  526. // Drop one of them and check that it's gone.
  527. db.DropFolder(ldb, "test1")
  528. expectedFolderList = []string{"test0"}
  529. actualFolderList = ldb.ListFolders()
  530. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  531. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  532. }
  533. if l := len(globalList(s0)); l != 3 {
  534. t.Errorf("Incorrect global length %d != 3 for s0", l)
  535. }
  536. if l := len(globalList(s1)); l != 0 {
  537. t.Errorf("Incorrect global length %d != 0 for s1", l)
  538. }
  539. }
  540. func TestGlobalNeedWithInvalid(t *testing.T) {
  541. ldb := db.NewLowlevel(backend.OpenMemory())
  542. defer ldb.Close()
  543. s := db.NewFileSet("test1", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  544. rem0 := fileList{
  545. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  546. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true},
  547. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  548. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  549. }
  550. replace(s, remoteDevice0, rem0)
  551. rem1 := fileList{
  552. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  553. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  554. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true},
  555. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true, ModifiedS: 10},
  556. }
  557. replace(s, remoteDevice1, rem1)
  558. total := fileList{
  559. // There's a valid copy of each file, so it should be merged
  560. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  561. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  562. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  563. // in conflict and older, but still wins as the other is invalid
  564. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  565. }
  566. need := fileList(needList(s, protocol.LocalDeviceID))
  567. if fmt.Sprint(need) != fmt.Sprint(total) {
  568. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
  569. }
  570. global := fileList(globalList(s))
  571. if fmt.Sprint(global) != fmt.Sprint(total) {
  572. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
  573. }
  574. }
  575. func TestLongPath(t *testing.T) {
  576. ldb := db.NewLowlevel(backend.OpenMemory())
  577. defer ldb.Close()
  578. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  579. var b bytes.Buffer
  580. for i := 0; i < 100; i++ {
  581. b.WriteString("012345678901234567890123456789012345678901234567890")
  582. }
  583. name := b.String() // 5000 characters
  584. local := []protocol.FileInfo{
  585. {Name: name, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  586. }
  587. replace(s, protocol.LocalDeviceID, local)
  588. gf := globalList(s)
  589. if l := len(gf); l != 1 {
  590. t.Fatalf("Incorrect len %d != 1 for global list", l)
  591. }
  592. if gf[0].Name != local[0].Name {
  593. t.Errorf("Incorrect long filename;\n%q !=\n%q",
  594. gf[0].Name, local[0].Name)
  595. }
  596. }
  597. func BenchmarkUpdateOneFile(b *testing.B) {
  598. local0 := fileList{
  599. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  600. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  601. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  602. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  603. // A longer name is more realistic and causes more allocations
  604. protocol.FileInfo{Name: "zajksdhaskjdh/askjdhaskjdashkajshd/kasjdhaskjdhaskdjhaskdjash/dkjashdaksjdhaskdjahskdjh", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  605. }
  606. be, err := backend.Open("testdata/benchmarkupdate.db", backend.TuningAuto)
  607. if err != nil {
  608. b.Fatal(err)
  609. }
  610. ldb := db.NewLowlevel(be)
  611. defer func() {
  612. ldb.Close()
  613. os.RemoveAll("testdata/benchmarkupdate.db")
  614. }()
  615. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  616. replace(m, protocol.LocalDeviceID, local0)
  617. l := local0[4:5]
  618. for i := 0; i < b.N; i++ {
  619. l[0].Version = l[0].Version.Update(myID)
  620. m.Update(protocol.LocalDeviceID, local0)
  621. }
  622. b.ReportAllocs()
  623. }
  624. func TestIndexID(t *testing.T) {
  625. ldb := db.NewLowlevel(backend.OpenMemory())
  626. defer ldb.Close()
  627. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  628. // The Index ID for some random device is zero by default.
  629. id := s.IndexID(remoteDevice0)
  630. if id != 0 {
  631. t.Errorf("index ID for remote device should default to zero, not %d", id)
  632. }
  633. // The Index ID for someone else should be settable
  634. s.SetIndexID(remoteDevice0, 42)
  635. id = s.IndexID(remoteDevice0)
  636. if id != 42 {
  637. t.Errorf("index ID for remote device should be remembered; got %d, expected %d", id, 42)
  638. }
  639. // Our own index ID should be generated randomly.
  640. id = s.IndexID(protocol.LocalDeviceID)
  641. if id == 0 {
  642. t.Errorf("index ID for local device should be random, not zero")
  643. }
  644. t.Logf("random index ID is 0x%016x", id)
  645. // But of course always the same after that.
  646. again := s.IndexID(protocol.LocalDeviceID)
  647. if again != id {
  648. t.Errorf("index ID changed; %d != %d", again, id)
  649. }
  650. }
  651. func TestDropFiles(t *testing.T) {
  652. ldb := db.NewLowlevel(backend.OpenMemory())
  653. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  654. local0 := fileList{
  655. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  656. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  657. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  658. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  659. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  660. }
  661. remote0 := fileList{
  662. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  663. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  664. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  665. }
  666. // Insert files
  667. m.Update(protocol.LocalDeviceID, local0)
  668. m.Update(remoteDevice0, remote0)
  669. // Check that they're there
  670. h := haveList(m, protocol.LocalDeviceID)
  671. if len(h) != len(local0) {
  672. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  673. }
  674. h = haveList(m, remoteDevice0)
  675. if len(h) != len(remote0) {
  676. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  677. }
  678. g := globalList(m)
  679. if len(g) != len(local0) {
  680. // local0 covers all files
  681. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(local0))
  682. }
  683. // Drop the local files and recheck
  684. m.Drop(protocol.LocalDeviceID)
  685. h = haveList(m, protocol.LocalDeviceID)
  686. if len(h) != 0 {
  687. t.Errorf("Incorrect number of files after drop, %d != %d", len(h), 0)
  688. }
  689. h = haveList(m, remoteDevice0)
  690. if len(h) != len(remote0) {
  691. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  692. }
  693. g = globalList(m)
  694. if len(g) != len(remote0) {
  695. // the ones in remote0 remain
  696. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(remote0))
  697. }
  698. }
  699. func TestIssue4701(t *testing.T) {
  700. ldb := db.NewLowlevel(backend.OpenMemory())
  701. defer ldb.Close()
  702. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  703. localHave := fileList{
  704. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  705. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, LocalFlags: protocol.FlagLocalIgnored},
  706. }
  707. s.Update(protocol.LocalDeviceID, localHave)
  708. if c := localSize(s); c.Files != 1 {
  709. t.Errorf("Expected 1 local file, got %v", c.Files)
  710. }
  711. if c := globalSize(s); c.Files != 1 {
  712. t.Errorf("Expected 1 global file, got %v", c.Files)
  713. }
  714. localHave[1].LocalFlags = 0
  715. s.Update(protocol.LocalDeviceID, localHave)
  716. if c := localSize(s); c.Files != 2 {
  717. t.Errorf("Expected 2 local files, got %v", c.Files)
  718. }
  719. if c := globalSize(s); c.Files != 2 {
  720. t.Errorf("Expected 2 global files, got %v", c.Files)
  721. }
  722. localHave[0].LocalFlags = protocol.FlagLocalIgnored
  723. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  724. s.Update(protocol.LocalDeviceID, localHave)
  725. if c := localSize(s); c.Files != 0 {
  726. t.Errorf("Expected 0 local files, got %v", c.Files)
  727. }
  728. if c := globalSize(s); c.Files != 0 {
  729. t.Errorf("Expected 0 global files, got %v", c.Files)
  730. }
  731. }
  732. func TestWithHaveSequence(t *testing.T) {
  733. ldb := db.NewLowlevel(backend.OpenMemory())
  734. defer ldb.Close()
  735. folder := "test"
  736. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  737. // The files must not be in alphabetical order
  738. localHave := fileList{
  739. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, RawInvalid: true},
  740. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  741. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  742. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  743. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  744. }
  745. replace(s, protocol.LocalDeviceID, localHave)
  746. i := 2
  747. snap := s.Snapshot()
  748. defer snap.Release()
  749. snap.WithHaveSequence(int64(i), func(fi db.FileIntf) bool {
  750. if f := fi.(protocol.FileInfo); !f.IsEquivalent(localHave[i-1], 0) {
  751. t.Fatalf("Got %v\nExpected %v", f, localHave[i-1])
  752. }
  753. i++
  754. return true
  755. })
  756. }
  757. func TestStressWithHaveSequence(t *testing.T) {
  758. // This races two loops against each other: one that contiously does
  759. // updates, and one that continuously does sequence walks. The test fails
  760. // if the sequence walker sees a discontinuity.
  761. if testing.Short() {
  762. t.Skip("Takes a long time")
  763. }
  764. ldb := db.NewLowlevel(backend.OpenMemory())
  765. defer ldb.Close()
  766. folder := "test"
  767. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  768. var localHave []protocol.FileInfo
  769. for i := 0; i < 100; i++ {
  770. localHave = append(localHave, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Blocks: genBlocks(i * 10)})
  771. }
  772. done := make(chan struct{})
  773. t0 := time.Now()
  774. go func() {
  775. for time.Since(t0) < 10*time.Second {
  776. for j, f := range localHave {
  777. localHave[j].Version = f.Version.Update(42)
  778. }
  779. s.Update(protocol.LocalDeviceID, localHave)
  780. }
  781. close(done)
  782. }()
  783. var prevSeq int64
  784. loop:
  785. for {
  786. select {
  787. case <-done:
  788. break loop
  789. default:
  790. }
  791. snap := s.Snapshot()
  792. snap.WithHaveSequence(prevSeq+1, func(fi db.FileIntf) bool {
  793. if fi.SequenceNo() < prevSeq+1 {
  794. t.Fatal("Skipped ", prevSeq+1, fi.SequenceNo())
  795. }
  796. prevSeq = fi.SequenceNo()
  797. return true
  798. })
  799. snap.Release()
  800. }
  801. }
  802. func TestIssue4925(t *testing.T) {
  803. ldb := db.NewLowlevel(backend.OpenMemory())
  804. defer ldb.Close()
  805. folder := "test"
  806. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  807. localHave := fileList{
  808. protocol.FileInfo{Name: "dir"},
  809. protocol.FileInfo{Name: "dir.file"},
  810. protocol.FileInfo{Name: "dir/file"},
  811. }
  812. replace(s, protocol.LocalDeviceID, localHave)
  813. for _, prefix := range []string{"dir", "dir/"} {
  814. pl := haveListPrefixed(s, protocol.LocalDeviceID, prefix)
  815. if l := len(pl); l != 2 {
  816. t.Errorf("Expected 2, got %v local items below %v", l, prefix)
  817. }
  818. pl = globalListPrefixed(s, prefix)
  819. if l := len(pl); l != 2 {
  820. t.Errorf("Expected 2, got %v global items below %v", l, prefix)
  821. }
  822. }
  823. }
  824. func TestMoveGlobalBack(t *testing.T) {
  825. ldb := db.NewLowlevel(backend.OpenMemory())
  826. defer ldb.Close()
  827. folder := "test"
  828. file := "foo"
  829. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  830. localHave := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Blocks: genBlocks(1), ModifiedS: 10, Size: 1}}
  831. remote0Have := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}, {ID: remoteDevice0.Short(), Value: 1}}}, Blocks: genBlocks(2), ModifiedS: 0, Size: 2}}
  832. s.Update(protocol.LocalDeviceID, localHave)
  833. s.Update(remoteDevice0, remote0Have)
  834. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  835. t.Error("Expected 1 local need, got", need)
  836. } else if !need[0].IsEquivalent(remote0Have[0], 0) {
  837. t.Errorf("Local need incorrect;\n A: %v !=\n E: %v", need[0], remote0Have[0])
  838. }
  839. if need := needList(s, remoteDevice0); len(need) != 0 {
  840. t.Error("Expected no need for remote 0, got", need)
  841. }
  842. ls := localSize(s)
  843. if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
  844. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  845. }
  846. gs := globalSize(s)
  847. if globalBytes := remote0Have[0].Size; gs.Bytes != globalBytes {
  848. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  849. }
  850. // That's what happens when something becomes unignored or something.
  851. // In any case it will be moved back from first spot in the global list
  852. // which is the scenario to be tested here.
  853. remote0Have[0].Version = remote0Have[0].Version.Update(remoteDevice0.Short()).DropOthers(remoteDevice0.Short())
  854. s.Update(remoteDevice0, remote0Have)
  855. if need := needList(s, remoteDevice0); len(need) != 1 {
  856. t.Error("Expected 1 need for remote 0, got", need)
  857. } else if !need[0].IsEquivalent(localHave[0], 0) {
  858. t.Errorf("Need for remote 0 incorrect;\n A: %v !=\n E: %v", need[0], localHave[0])
  859. }
  860. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  861. t.Error("Expected no local need, got", need)
  862. }
  863. ls = localSize(s)
  864. if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
  865. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  866. }
  867. gs = globalSize(s)
  868. if globalBytes := localHave[0].Size; gs.Bytes != globalBytes {
  869. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  870. }
  871. }
  872. // TestIssue5007 checks, that updating the local device with an invalid file
  873. // info with the newest version does indeed remove that file from the list of
  874. // needed files.
  875. // https://github.com/syncthing/syncthing/issues/5007
  876. func TestIssue5007(t *testing.T) {
  877. ldb := db.NewLowlevel(backend.OpenMemory())
  878. defer ldb.Close()
  879. folder := "test"
  880. file := "foo"
  881. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  882. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}}}
  883. s.Update(remoteDevice0, fs)
  884. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  885. t.Fatal("Expected 1 local need, got", need)
  886. } else if !need[0].IsEquivalent(fs[0], 0) {
  887. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  888. }
  889. fs[0].LocalFlags = protocol.FlagLocalIgnored
  890. s.Update(protocol.LocalDeviceID, fs)
  891. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  892. t.Fatal("Expected no local need, got", need)
  893. }
  894. }
  895. // TestNeedDeleted checks that a file that doesn't exist locally isn't needed
  896. // when the global file is deleted.
  897. func TestNeedDeleted(t *testing.T) {
  898. ldb := db.NewLowlevel(backend.OpenMemory())
  899. defer ldb.Close()
  900. folder := "test"
  901. file := "foo"
  902. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  903. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Deleted: true}}
  904. s.Update(remoteDevice0, fs)
  905. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  906. t.Fatal("Expected no local need, got", need)
  907. }
  908. fs[0].Deleted = false
  909. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  910. s.Update(remoteDevice0, fs)
  911. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  912. t.Fatal("Expected 1 local need, got", need)
  913. } else if !need[0].IsEquivalent(fs[0], 0) {
  914. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  915. }
  916. fs[0].Deleted = true
  917. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  918. s.Update(remoteDevice0, fs)
  919. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  920. t.Fatal("Expected no local need, got", need)
  921. }
  922. }
  923. func TestReceiveOnlyAccounting(t *testing.T) {
  924. ldb := db.NewLowlevel(backend.OpenMemory())
  925. defer ldb.Close()
  926. folder := "test"
  927. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  928. local := protocol.DeviceID{1}
  929. remote := protocol.DeviceID{2}
  930. // Three files that have been created by the remote device
  931. version := protocol.Vector{Counters: []protocol.Counter{{ID: remote.Short(), Value: 1}}}
  932. files := fileList{
  933. protocol.FileInfo{Name: "f1", Size: 10, Sequence: 1, Version: version},
  934. protocol.FileInfo{Name: "f2", Size: 10, Sequence: 1, Version: version},
  935. protocol.FileInfo{Name: "f3", Size: 10, Sequence: 1, Version: version},
  936. }
  937. // We have synced them locally
  938. replace(s, protocol.LocalDeviceID, files)
  939. replace(s, remote, files)
  940. if n := localSize(s).Files; n != 3 {
  941. t.Fatal("expected 3 local files initially, not", n)
  942. }
  943. if n := localSize(s).Bytes; n != 30 {
  944. t.Fatal("expected 30 local bytes initially, not", n)
  945. }
  946. if n := globalSize(s).Files; n != 3 {
  947. t.Fatal("expected 3 global files initially, not", n)
  948. }
  949. if n := globalSize(s).Bytes; n != 30 {
  950. t.Fatal("expected 30 global bytes initially, not", n)
  951. }
  952. if n := receiveOnlyChangedSize(s).Files; n != 0 {
  953. t.Fatal("expected 0 receive only changed files initially, not", n)
  954. }
  955. if n := receiveOnlyChangedSize(s).Bytes; n != 0 {
  956. t.Fatal("expected 0 receive only changed bytes initially, not", n)
  957. }
  958. // Detected a local change in a receive only folder
  959. changed := files[0]
  960. changed.Version = changed.Version.Update(local.Short())
  961. changed.Size = 100
  962. changed.ModifiedBy = local.Short()
  963. changed.LocalFlags = protocol.FlagLocalReceiveOnly
  964. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{changed})
  965. // Check that we see the files
  966. if n := localSize(s).Files; n != 3 {
  967. t.Fatal("expected 3 local files after local change, not", n)
  968. }
  969. if n := localSize(s).Bytes; n != 120 {
  970. t.Fatal("expected 120 local bytes after local change, not", n)
  971. }
  972. if n := globalSize(s).Files; n != 3 {
  973. t.Fatal("expected 3 global files after local change, not", n)
  974. }
  975. if n := globalSize(s).Bytes; n != 30 {
  976. t.Fatal("expected 30 global files after local change, not", n)
  977. }
  978. if n := receiveOnlyChangedSize(s).Files; n != 1 {
  979. t.Fatal("expected 1 receive only changed file after local change, not", n)
  980. }
  981. if n := receiveOnlyChangedSize(s).Bytes; n != 100 {
  982. t.Fatal("expected 100 receive only changed btyes after local change, not", n)
  983. }
  984. // Fake a revert. That's a two step process, first converting our
  985. // changed file into a less preferred variant, then pulling down the old
  986. // version.
  987. changed.Version = protocol.Vector{}
  988. changed.LocalFlags &^= protocol.FlagLocalReceiveOnly
  989. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{changed})
  990. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{files[0]})
  991. // Check that we see the files, same data as initially
  992. if n := localSize(s).Files; n != 3 {
  993. t.Fatal("expected 3 local files after revert, not", n)
  994. }
  995. if n := localSize(s).Bytes; n != 30 {
  996. t.Fatal("expected 30 local bytes after revert, not", n)
  997. }
  998. if n := globalSize(s).Files; n != 3 {
  999. t.Fatal("expected 3 global files after revert, not", n)
  1000. }
  1001. if n := globalSize(s).Bytes; n != 30 {
  1002. t.Fatal("expected 30 global bytes after revert, not", n)
  1003. }
  1004. if n := receiveOnlyChangedSize(s).Files; n != 0 {
  1005. t.Fatal("expected 0 receive only changed files after revert, not", n)
  1006. }
  1007. if n := receiveOnlyChangedSize(s).Bytes; n != 0 {
  1008. t.Fatal("expected 0 receive only changed bytes after revert, not", n)
  1009. }
  1010. }
  1011. func TestNeedAfterUnignore(t *testing.T) {
  1012. ldb := db.NewLowlevel(backend.OpenMemory())
  1013. defer ldb.Close()
  1014. folder := "test"
  1015. file := "foo"
  1016. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1017. remID := remoteDevice0.Short()
  1018. // Initial state: Devices in sync, locally ignored
  1019. local := protocol.FileInfo{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: remID, Value: 1}, {ID: myID, Value: 1}}}, ModifiedS: 10}
  1020. local.SetIgnored(myID)
  1021. remote := protocol.FileInfo{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: remID, Value: 1}, {ID: myID, Value: 1}}}, ModifiedS: 10}
  1022. s.Update(protocol.LocalDeviceID, fileList{local})
  1023. s.Update(remoteDevice0, fileList{remote})
  1024. // Unignore locally -> conflicting changes. Remote is newer, thus winning.
  1025. local.Version = local.Version.Update(myID)
  1026. local.Version = local.Version.DropOthers(myID)
  1027. local.LocalFlags = 0
  1028. local.ModifiedS = 0
  1029. s.Update(protocol.LocalDeviceID, fileList{local})
  1030. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  1031. t.Fatal("Expected one local need, got", need)
  1032. } else if !need[0].IsEquivalent(remote, 0) {
  1033. t.Fatalf("Got %v, expected %v", need[0], remote)
  1034. }
  1035. }
  1036. func TestRemoteInvalidNotAccounted(t *testing.T) {
  1037. // Remote files with the invalid bit should not count.
  1038. ldb := db.NewLowlevel(backend.OpenMemory())
  1039. defer ldb.Close()
  1040. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1041. files := []protocol.FileInfo{
  1042. {Name: "a", Size: 1234, Sequence: 42, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}}, // valid, should count
  1043. {Name: "b", Size: 1234, Sequence: 43, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, RawInvalid: true}, // invalid, doesn't count
  1044. }
  1045. s.Update(remoteDevice0, files)
  1046. global := globalSize(s)
  1047. if global.Files != 1 {
  1048. t.Error("Expected one file in global size, not", global.Files)
  1049. }
  1050. if global.Bytes != 1234 {
  1051. t.Error("Expected 1234 bytes in global size, not", global.Bytes)
  1052. }
  1053. }
  1054. func TestNeedWithNewerInvalid(t *testing.T) {
  1055. ldb := db.NewLowlevel(backend.OpenMemory())
  1056. defer ldb.Close()
  1057. s := db.NewFileSet("default", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1058. rem0ID := remoteDevice0.Short()
  1059. rem1ID := remoteDevice1.Short()
  1060. // Initial state: file present on rem0 and rem1, but not locally.
  1061. file := protocol.FileInfo{Name: "foo"}
  1062. file.Version = file.Version.Update(rem0ID)
  1063. s.Update(remoteDevice0, fileList{file})
  1064. s.Update(remoteDevice1, fileList{file})
  1065. need := needList(s, protocol.LocalDeviceID)
  1066. if len(need) != 1 {
  1067. t.Fatal("Locally missing file should be needed")
  1068. }
  1069. if !need[0].IsEquivalent(file, 0) {
  1070. t.Fatalf("Got needed file %v, expected %v", need[0], file)
  1071. }
  1072. // rem1 sends an invalid file with increased version
  1073. inv := file
  1074. inv.Version = inv.Version.Update(rem1ID)
  1075. inv.RawInvalid = true
  1076. s.Update(remoteDevice1, fileList{inv})
  1077. // We still have an old file, we need the newest valid file
  1078. need = needList(s, protocol.LocalDeviceID)
  1079. if len(need) != 1 {
  1080. t.Fatal("Locally missing file should be needed regardless of invalid files")
  1081. }
  1082. if !need[0].IsEquivalent(file, 0) {
  1083. t.Fatalf("Got needed file %v, expected %v", need[0], file)
  1084. }
  1085. }
  1086. func TestNeedAfterDeviceRemove(t *testing.T) {
  1087. ldb := db.NewLowlevel(backend.OpenMemory())
  1088. defer ldb.Close()
  1089. file := "foo"
  1090. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1091. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}}}
  1092. s.Update(protocol.LocalDeviceID, fs)
  1093. fs[0].Version = fs[0].Version.Update(myID)
  1094. s.Update(remoteDevice0, fs)
  1095. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  1096. t.Fatal("Expected one local need, got", need)
  1097. }
  1098. s.Drop(remoteDevice0)
  1099. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  1100. t.Fatal("Expected no local need, got", need)
  1101. }
  1102. }
  1103. func TestCaseSensitive(t *testing.T) {
  1104. // Normal case sensitive lookup should work
  1105. ldb := db.NewLowlevel(backend.OpenMemory())
  1106. defer ldb.Close()
  1107. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1108. local := []protocol.FileInfo{
  1109. {Name: filepath.FromSlash("D1/f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1110. {Name: filepath.FromSlash("F1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1111. {Name: filepath.FromSlash("d1/F1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1112. {Name: filepath.FromSlash("d1/f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1113. {Name: filepath.FromSlash("f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1114. }
  1115. replace(s, protocol.LocalDeviceID, local)
  1116. gf := globalList(s)
  1117. if l := len(gf); l != len(local) {
  1118. t.Fatalf("Incorrect len %d != %d for global list", l, len(local))
  1119. }
  1120. for i := range local {
  1121. if gf[i].Name != local[i].Name {
  1122. t.Errorf("Incorrect filename;\n%q !=\n%q",
  1123. gf[i].Name, local[i].Name)
  1124. }
  1125. }
  1126. }
  1127. func TestSequenceIndex(t *testing.T) {
  1128. // This test attempts to verify correct operation of the sequence index.
  1129. // It's a stress test and needs to run for a long time, but we don't
  1130. // really have time for that in normal builds.
  1131. runtime := time.Minute
  1132. if testing.Short() {
  1133. runtime = time.Second
  1134. }
  1135. // Set up a db and a few files that we will manipulate.
  1136. ldb := db.NewLowlevel(backend.OpenMemory())
  1137. defer ldb.Close()
  1138. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1139. local := []protocol.FileInfo{
  1140. {Name: filepath.FromSlash("banana"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1141. {Name: filepath.FromSlash("pineapple"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1142. {Name: filepath.FromSlash("orange"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1143. {Name: filepath.FromSlash("apple"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1144. {Name: filepath.FromSlash("jackfruit"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1145. }
  1146. // Start a background routine that makes updates to these files as fast
  1147. // as it can. We always update the same files in the same order.
  1148. done := make(chan struct{})
  1149. defer close(done)
  1150. go func() {
  1151. for {
  1152. select {
  1153. case <-done:
  1154. return
  1155. default:
  1156. }
  1157. for i := range local {
  1158. local[i].Version = local[i].Version.Update(42)
  1159. }
  1160. s.Update(protocol.LocalDeviceID, local)
  1161. }
  1162. }()
  1163. // Start a routine to walk the sequence index and inspect the result.
  1164. seen := make(map[string]db.FileIntf)
  1165. latest := make([]db.FileIntf, 0, len(local))
  1166. var seq int64
  1167. t0 := time.Now()
  1168. for time.Since(t0) < runtime {
  1169. // Walk the changes since our last iteration. This should give is
  1170. // one instance each of the files that are changed all the time, or
  1171. // a subset of those files if we manage to run before a complete
  1172. // update has happened since our last iteration.
  1173. latest = latest[:0]
  1174. snap := s.Snapshot()
  1175. snap.WithHaveSequence(seq+1, func(f db.FileIntf) bool {
  1176. seen[f.FileName()] = f
  1177. latest = append(latest, f)
  1178. seq = f.SequenceNo()
  1179. return true
  1180. })
  1181. snap.Release()
  1182. // Calculate the spread in sequence number.
  1183. var max, min int64
  1184. for _, v := range seen {
  1185. s := v.SequenceNo()
  1186. if max == 0 || max < s {
  1187. max = s
  1188. }
  1189. if min == 0 || min > s {
  1190. min = s
  1191. }
  1192. }
  1193. // We shouldn't see a spread larger than the number of files, as
  1194. // that would mean we have missed updates. For example, if we were
  1195. // to see the following:
  1196. //
  1197. // banana N
  1198. // pineapple N+1
  1199. // orange N+2
  1200. // apple N+10
  1201. // jackfruit N+11
  1202. //
  1203. // that would mean that there have been updates to banana, pineapple
  1204. // and orange that we didn't see in this pass. If those files aren't
  1205. // updated again, those updates are permanently lost.
  1206. if max-min > int64(len(local)) {
  1207. for _, v := range seen {
  1208. t.Log("seen", v.FileName(), v.SequenceNo())
  1209. }
  1210. for _, v := range latest {
  1211. t.Log("latest", v.FileName(), v.SequenceNo())
  1212. }
  1213. t.Fatal("large spread")
  1214. }
  1215. time.Sleep(time.Millisecond)
  1216. }
  1217. }
  1218. func TestIgnoreAfterReceiveOnly(t *testing.T) {
  1219. ldb := db.NewLowlevel(backend.OpenMemory())
  1220. defer ldb.Close()
  1221. file := "foo"
  1222. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1223. fs := fileList{{
  1224. Name: file,
  1225. Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}},
  1226. LocalFlags: protocol.FlagLocalReceiveOnly,
  1227. }}
  1228. s.Update(protocol.LocalDeviceID, fs)
  1229. fs[0].LocalFlags = protocol.FlagLocalIgnored
  1230. s.Update(protocol.LocalDeviceID, fs)
  1231. snap := s.Snapshot()
  1232. defer snap.Release()
  1233. if f, ok := snap.Get(protocol.LocalDeviceID, file); !ok {
  1234. t.Error("File missing in db")
  1235. } else if f.IsReceiveOnlyChanged() {
  1236. t.Error("File is still receive-only changed")
  1237. } else if !f.IsIgnored() {
  1238. t.Error("File is not ignored")
  1239. }
  1240. }
  1241. func replace(fs *db.FileSet, device protocol.DeviceID, files []protocol.FileInfo) {
  1242. fs.Drop(device)
  1243. fs.Update(device, files)
  1244. }
  1245. func localSize(fs *db.FileSet) db.Counts {
  1246. snap := fs.Snapshot()
  1247. defer snap.Release()
  1248. return snap.LocalSize()
  1249. }
  1250. func globalSize(fs *db.FileSet) db.Counts {
  1251. snap := fs.Snapshot()
  1252. defer snap.Release()
  1253. return snap.GlobalSize()
  1254. }
  1255. func receiveOnlyChangedSize(fs *db.FileSet) db.Counts {
  1256. snap := fs.Snapshot()
  1257. defer snap.Release()
  1258. return snap.ReceiveOnlyChangedSize()
  1259. }