set_test.go 56 KB

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