set_test.go 59 KB

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