set_test.go 54 KB

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