set_test.go 52 KB

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