set_test.go 59 KB

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