set_test.go 59 KB

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