set_test.go 59 KB

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