set_test.go 60 KB

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