set_test.go 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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. "sort"
  12. "testing"
  13. "time"
  14. "github.com/d4l3k/messagediff"
  15. "github.com/syncthing/syncthing/lib/db"
  16. "github.com/syncthing/syncthing/lib/fs"
  17. "github.com/syncthing/syncthing/lib/protocol"
  18. )
  19. var remoteDevice0, remoteDevice1 protocol.DeviceID
  20. func init() {
  21. remoteDevice0, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  22. remoteDevice1, _ = protocol.DeviceIDFromString("I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU")
  23. }
  24. const myID = 1
  25. func genBlocks(n int) []protocol.BlockInfo {
  26. b := make([]protocol.BlockInfo, n)
  27. for i := range b {
  28. h := make([]byte, 32)
  29. for j := range h {
  30. h[j] = byte(i + j)
  31. }
  32. b[i].Size = int32(i)
  33. b[i].Hash = h
  34. }
  35. return b
  36. }
  37. func globalList(s *db.FileSet) []protocol.FileInfo {
  38. var fs []protocol.FileInfo
  39. s.WithGlobal(func(fi db.FileIntf) bool {
  40. f := fi.(protocol.FileInfo)
  41. fs = append(fs, f)
  42. return true
  43. })
  44. return fs
  45. }
  46. func globalListPrefixed(s *db.FileSet, prefix string) []db.FileInfoTruncated {
  47. var fs []db.FileInfoTruncated
  48. s.WithPrefixedGlobalTruncated(prefix, func(fi db.FileIntf) bool {
  49. f := fi.(db.FileInfoTruncated)
  50. fs = append(fs, f)
  51. return true
  52. })
  53. return fs
  54. }
  55. func haveList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  56. var fs []protocol.FileInfo
  57. s.WithHave(n, func(fi db.FileIntf) bool {
  58. f := fi.(protocol.FileInfo)
  59. fs = append(fs, f)
  60. return true
  61. })
  62. return fs
  63. }
  64. func haveListPrefixed(s *db.FileSet, n protocol.DeviceID, prefix string) []db.FileInfoTruncated {
  65. var fs []db.FileInfoTruncated
  66. s.WithPrefixedHaveTruncated(n, prefix, func(fi db.FileIntf) bool {
  67. f := fi.(db.FileInfoTruncated)
  68. fs = append(fs, f)
  69. return true
  70. })
  71. return fs
  72. }
  73. func needList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  74. var fs []protocol.FileInfo
  75. s.WithNeed(n, func(fi db.FileIntf) bool {
  76. f := fi.(protocol.FileInfo)
  77. fs = append(fs, f)
  78. return true
  79. })
  80. return fs
  81. }
  82. type fileList []protocol.FileInfo
  83. func (l fileList) Len() int {
  84. return len(l)
  85. }
  86. func (l fileList) Less(a, b int) bool {
  87. return l[a].Name < l[b].Name
  88. }
  89. func (l fileList) Swap(a, b int) {
  90. l[a], l[b] = l[b], l[a]
  91. }
  92. func (l fileList) String() string {
  93. var b bytes.Buffer
  94. b.WriteString("[]protocol.FileList{\n")
  95. for _, f := range l {
  96. fmt.Fprintf(&b, " %q: #%v, %d bytes, %d blocks, perms=%o\n", f.Name, f.Version, f.Size, len(f.Blocks), f.Permissions)
  97. }
  98. b.WriteString("}")
  99. return b.String()
  100. }
  101. func TestGlobalSet(t *testing.T) {
  102. ldb := db.OpenMemory()
  103. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  104. local0 := fileList{
  105. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  106. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  107. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  108. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  109. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  110. }
  111. local1 := fileList{
  112. protocol.FileInfo{Name: "a", Sequence: 6, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  113. protocol.FileInfo{Name: "b", Sequence: 7, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  114. protocol.FileInfo{Name: "c", Sequence: 8, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  115. protocol.FileInfo{Name: "d", Sequence: 9, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  116. protocol.FileInfo{Name: "z", Sequence: 10, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  117. }
  118. localTot := fileList{
  119. local1[0],
  120. local1[1],
  121. local1[2],
  122. local1[3],
  123. protocol.FileInfo{Name: "z", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  124. }
  125. remote0 := fileList{
  126. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  127. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  128. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  129. }
  130. remote1 := fileList{
  131. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(6)},
  132. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(7)},
  133. }
  134. remoteTot := fileList{
  135. remote0[0],
  136. remote1[0],
  137. remote0[2],
  138. remote1[1],
  139. }
  140. expectedGlobal := fileList{
  141. remote0[0], // a
  142. remote1[0], // b
  143. remote0[2], // c
  144. localTot[3], // d
  145. remote1[1], // e
  146. localTot[4], // z
  147. }
  148. expectedLocalNeed := fileList{
  149. remote1[0],
  150. remote0[2],
  151. remote1[1],
  152. }
  153. expectedRemoteNeed := fileList{
  154. local0[3],
  155. }
  156. replace(m, protocol.LocalDeviceID, local0)
  157. replace(m, protocol.LocalDeviceID, local1)
  158. replace(m, remoteDevice0, remote0)
  159. m.Update(remoteDevice0, remote1)
  160. g := fileList(globalList(m))
  161. sort.Sort(g)
  162. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
  163. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal)
  164. }
  165. globalFiles, globalDirectories, globalDeleted, globalBytes := int32(0), int32(0), int32(0), int64(0)
  166. for _, f := range g {
  167. if f.IsInvalid() {
  168. continue
  169. }
  170. switch {
  171. case f.IsDeleted():
  172. globalDeleted++
  173. case f.IsDirectory():
  174. globalDirectories++
  175. default:
  176. globalFiles++
  177. }
  178. globalBytes += f.FileSize()
  179. }
  180. gs := m.GlobalSize()
  181. if gs.Files != globalFiles {
  182. t.Errorf("Incorrect GlobalSize files; %d != %d", gs.Files, globalFiles)
  183. }
  184. if gs.Directories != globalDirectories {
  185. t.Errorf("Incorrect GlobalSize directories; %d != %d", gs.Directories, globalDirectories)
  186. }
  187. if gs.Deleted != globalDeleted {
  188. t.Errorf("Incorrect GlobalSize deleted; %d != %d", gs.Deleted, globalDeleted)
  189. }
  190. if gs.Bytes != globalBytes {
  191. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  192. }
  193. h := fileList(haveList(m, protocol.LocalDeviceID))
  194. sort.Sort(h)
  195. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  196. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot)
  197. }
  198. haveFiles, haveDirectories, haveDeleted, haveBytes := int32(0), int32(0), int32(0), int64(0)
  199. for _, f := range h {
  200. if f.IsInvalid() {
  201. continue
  202. }
  203. switch {
  204. case f.IsDeleted():
  205. haveDeleted++
  206. case f.IsDirectory():
  207. haveDirectories++
  208. default:
  209. haveFiles++
  210. }
  211. haveBytes += f.FileSize()
  212. }
  213. ls := m.LocalSize()
  214. if ls.Files != haveFiles {
  215. t.Errorf("Incorrect LocalSize files; %d != %d", ls.Files, haveFiles)
  216. }
  217. if ls.Directories != haveDirectories {
  218. t.Errorf("Incorrect LocalSize directories; %d != %d", ls.Directories, haveDirectories)
  219. }
  220. if ls.Deleted != haveDeleted {
  221. t.Errorf("Incorrect LocalSize deleted; %d != %d", ls.Deleted, haveDeleted)
  222. }
  223. if ls.Bytes != haveBytes {
  224. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  225. }
  226. h = fileList(haveList(m, remoteDevice0))
  227. sort.Sort(h)
  228. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  229. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, remoteTot)
  230. }
  231. n := fileList(needList(m, protocol.LocalDeviceID))
  232. sort.Sort(n)
  233. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  234. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedLocalNeed)
  235. }
  236. n = fileList(needList(m, remoteDevice0))
  237. sort.Sort(n)
  238. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  239. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  240. }
  241. f, ok := m.Get(protocol.LocalDeviceID, "b")
  242. if !ok {
  243. t.Error("Unexpectedly not OK")
  244. }
  245. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  246. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  247. }
  248. f, ok = m.Get(remoteDevice0, "b")
  249. if !ok {
  250. t.Error("Unexpectedly not OK")
  251. }
  252. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  253. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  254. }
  255. f, ok = m.GetGlobal("b")
  256. if !ok {
  257. t.Error("Unexpectedly not OK")
  258. }
  259. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  260. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  261. }
  262. f, ok = m.Get(protocol.LocalDeviceID, "zz")
  263. if ok {
  264. t.Error("Unexpectedly OK")
  265. }
  266. if f.Name != "" {
  267. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  268. }
  269. f, ok = m.GetGlobal("zz")
  270. if ok {
  271. t.Error("Unexpectedly OK")
  272. }
  273. if f.Name != "" {
  274. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  275. }
  276. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  277. a := m.Availability("a")
  278. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  279. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  280. }
  281. a = m.Availability("b")
  282. if len(a) != 1 || a[0] != remoteDevice0 {
  283. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  284. }
  285. a = m.Availability("d")
  286. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  287. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  288. }
  289. }
  290. func TestNeedWithInvalid(t *testing.T) {
  291. ldb := db.OpenMemory()
  292. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  293. localHave := fileList{
  294. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  295. }
  296. remote0Have := fileList{
  297. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  298. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  299. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  300. }
  301. remote1Have := fileList{
  302. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  303. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  304. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  305. }
  306. expectedNeed := fileList{
  307. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  308. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  309. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  310. }
  311. replace(s, protocol.LocalDeviceID, localHave)
  312. replace(s, remoteDevice0, remote0Have)
  313. replace(s, remoteDevice1, remote1Have)
  314. need := fileList(needList(s, protocol.LocalDeviceID))
  315. sort.Sort(need)
  316. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  317. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  318. }
  319. }
  320. func TestUpdateToInvalid(t *testing.T) {
  321. ldb := db.OpenMemory()
  322. folder := "test"
  323. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  324. f := db.NewBlockFinder(ldb)
  325. localHave := fileList{
  326. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  327. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  328. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), LocalFlags: protocol.FlagLocalIgnored},
  329. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  330. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, LocalFlags: protocol.FlagLocalIgnored},
  331. }
  332. replace(s, protocol.LocalDeviceID, localHave)
  333. have := fileList(haveList(s, protocol.LocalDeviceID))
  334. sort.Sort(have)
  335. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  336. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  337. }
  338. oldBlockHash := localHave[1].Blocks[0].Hash
  339. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  340. localHave[1].Blocks = nil
  341. localHave[4].LocalFlags = 0
  342. localHave[4].Blocks = genBlocks(3)
  343. s.Update(protocol.LocalDeviceID, append(fileList{}, localHave[1], localHave[4]))
  344. have = fileList(haveList(s, protocol.LocalDeviceID))
  345. sort.Sort(have)
  346. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  347. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  348. }
  349. f.Iterate([]string{folder}, oldBlockHash, func(folder, file string, index int32) bool {
  350. if file == localHave[1].Name {
  351. t.Errorf("Found unexpected block in blockmap for invalidated file")
  352. return true
  353. }
  354. return false
  355. })
  356. if !f.Iterate([]string{folder}, localHave[4].Blocks[0].Hash, func(folder, file string, index int32) bool {
  357. if file == localHave[4].Name {
  358. return true
  359. }
  360. return false
  361. }) {
  362. t.Errorf("First block of un-invalidated file is missing from blockmap")
  363. }
  364. }
  365. func TestInvalidAvailability(t *testing.T) {
  366. ldb := db.OpenMemory()
  367. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  368. remote0Have := fileList{
  369. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  370. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  371. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  372. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  373. }
  374. remote1Have := fileList{
  375. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  376. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  377. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  378. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  379. }
  380. replace(s, remoteDevice0, remote0Have)
  381. replace(s, remoteDevice1, remote1Have)
  382. if av := s.Availability("both"); len(av) != 2 {
  383. t.Error("Incorrect availability for 'both':", av)
  384. }
  385. if av := s.Availability("r0only"); len(av) != 1 || av[0] != remoteDevice0 {
  386. t.Error("Incorrect availability for 'r0only':", av)
  387. }
  388. if av := s.Availability("r1only"); len(av) != 1 || av[0] != remoteDevice1 {
  389. t.Error("Incorrect availability for 'r1only':", av)
  390. }
  391. if av := s.Availability("none"); len(av) != 0 {
  392. t.Error("Incorrect availability for 'none':", av)
  393. }
  394. }
  395. func TestGlobalReset(t *testing.T) {
  396. ldb := db.OpenMemory()
  397. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  398. local := []protocol.FileInfo{
  399. {Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  400. {Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  401. {Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  402. {Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  403. }
  404. remote := []protocol.FileInfo{
  405. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  406. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  407. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  408. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  409. }
  410. replace(m, protocol.LocalDeviceID, local)
  411. g := globalList(m)
  412. sort.Sort(fileList(g))
  413. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  414. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  415. }
  416. replace(m, remoteDevice0, remote)
  417. replace(m, remoteDevice0, nil)
  418. g = globalList(m)
  419. sort.Sort(fileList(g))
  420. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  421. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  422. }
  423. }
  424. func TestNeed(t *testing.T) {
  425. ldb := db.OpenMemory()
  426. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  427. local := []protocol.FileInfo{
  428. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  429. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  430. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  431. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  432. }
  433. remote := []protocol.FileInfo{
  434. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  435. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  436. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  437. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  438. }
  439. shouldNeed := []protocol.FileInfo{
  440. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  441. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  442. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  443. }
  444. replace(m, protocol.LocalDeviceID, local)
  445. replace(m, remoteDevice0, remote)
  446. need := needList(m, protocol.LocalDeviceID)
  447. sort.Sort(fileList(need))
  448. sort.Sort(fileList(shouldNeed))
  449. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  450. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  451. }
  452. }
  453. func TestSequence(t *testing.T) {
  454. ldb := db.OpenMemory()
  455. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  456. local1 := []protocol.FileInfo{
  457. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  458. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  459. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  460. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  461. }
  462. local2 := []protocol.FileInfo{
  463. local1[0],
  464. // [1] deleted
  465. local1[2],
  466. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  467. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  468. }
  469. replace(m, protocol.LocalDeviceID, local1)
  470. c0 := m.Sequence(protocol.LocalDeviceID)
  471. replace(m, protocol.LocalDeviceID, local2)
  472. c1 := m.Sequence(protocol.LocalDeviceID)
  473. if !(c1 > c0) {
  474. t.Fatal("Local version number should have incremented")
  475. }
  476. }
  477. func TestListDropFolder(t *testing.T) {
  478. ldb := db.OpenMemory()
  479. s0 := db.NewFileSet("test0", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  480. local1 := []protocol.FileInfo{
  481. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  482. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  483. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  484. }
  485. replace(s0, protocol.LocalDeviceID, local1)
  486. s1 := db.NewFileSet("test1", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  487. local2 := []protocol.FileInfo{
  488. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  489. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  490. {Name: "f", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  491. }
  492. replace(s1, remoteDevice0, local2)
  493. // Check that we have both folders and their data is in the global list
  494. expectedFolderList := []string{"test0", "test1"}
  495. actualFolderList := ldb.ListFolders()
  496. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  497. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  498. }
  499. if l := len(globalList(s0)); l != 3 {
  500. t.Errorf("Incorrect global length %d != 3 for s0", l)
  501. }
  502. if l := len(globalList(s1)); l != 3 {
  503. t.Errorf("Incorrect global length %d != 3 for s1", l)
  504. }
  505. // Drop one of them and check that it's gone.
  506. db.DropFolder(ldb, "test1")
  507. expectedFolderList = []string{"test0"}
  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 != 0 {
  516. t.Errorf("Incorrect global length %d != 0 for s1", l)
  517. }
  518. }
  519. func TestGlobalNeedWithInvalid(t *testing.T) {
  520. ldb := db.OpenMemory()
  521. s := db.NewFileSet("test1", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  522. rem0 := fileList{
  523. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  524. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true},
  525. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  526. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  527. }
  528. replace(s, remoteDevice0, rem0)
  529. rem1 := fileList{
  530. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  531. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  532. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true},
  533. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true, ModifiedS: 10},
  534. }
  535. replace(s, remoteDevice1, rem1)
  536. total := fileList{
  537. // There's a valid copy of each file, so it should be merged
  538. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  539. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  540. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  541. // in conflict and older, but still wins as the other is invalid
  542. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  543. }
  544. need := fileList(needList(s, protocol.LocalDeviceID))
  545. if fmt.Sprint(need) != fmt.Sprint(total) {
  546. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
  547. }
  548. global := fileList(globalList(s))
  549. if fmt.Sprint(global) != fmt.Sprint(total) {
  550. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
  551. }
  552. }
  553. func TestLongPath(t *testing.T) {
  554. ldb := db.OpenMemory()
  555. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  556. var b bytes.Buffer
  557. for i := 0; i < 100; i++ {
  558. b.WriteString("012345678901234567890123456789012345678901234567890")
  559. }
  560. name := b.String() // 5000 characters
  561. local := []protocol.FileInfo{
  562. {Name: string(name), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  563. }
  564. replace(s, protocol.LocalDeviceID, local)
  565. gf := globalList(s)
  566. if l := len(gf); l != 1 {
  567. t.Fatalf("Incorrect len %d != 1 for global list", l)
  568. }
  569. if gf[0].Name != local[0].Name {
  570. t.Errorf("Incorrect long filename;\n%q !=\n%q",
  571. gf[0].Name, local[0].Name)
  572. }
  573. }
  574. func TestCommitted(t *testing.T) {
  575. // Verify that the Committed counter increases when we change things and
  576. // doesn't increase when we don't.
  577. ldb := db.OpenMemory()
  578. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  579. local := []protocol.FileInfo{
  580. {Name: string("file"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  581. }
  582. // Adding a file should increase the counter
  583. c0 := ldb.Committed()
  584. replace(s, protocol.LocalDeviceID, local)
  585. c1 := ldb.Committed()
  586. if c1 <= c0 {
  587. t.Errorf("committed data didn't increase; %d <= %d", c1, c0)
  588. }
  589. // Updating with something identical should not do anything
  590. s.Update(protocol.LocalDeviceID, local)
  591. c2 := ldb.Committed()
  592. if c2 > c1 {
  593. t.Errorf("replace with same contents should do nothing but %d > %d", c2, c1)
  594. }
  595. }
  596. func BenchmarkUpdateOneFile(b *testing.B) {
  597. local0 := fileList{
  598. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  599. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  600. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  601. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  602. // A longer name is more realistic and causes more allocations
  603. protocol.FileInfo{Name: "zajksdhaskjdh/askjdhaskjdashkajshd/kasjdhaskjdhaskdjhaskdjash/dkjashdaksjdhaskdjahskdjh", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  604. }
  605. ldb, err := db.Open("testdata/benchmarkupdate.db")
  606. if err != nil {
  607. b.Fatal(err)
  608. }
  609. defer func() {
  610. ldb.Close()
  611. os.RemoveAll("testdata/benchmarkupdate.db")
  612. }()
  613. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  614. replace(m, protocol.LocalDeviceID, local0)
  615. l := local0[4:5]
  616. for i := 0; i < b.N; i++ {
  617. l[0].Version = l[0].Version.Update(myID)
  618. m.Update(protocol.LocalDeviceID, local0)
  619. }
  620. b.ReportAllocs()
  621. }
  622. func TestIndexID(t *testing.T) {
  623. ldb := db.OpenMemory()
  624. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  625. // The Index ID for some random device is zero by default.
  626. id := s.IndexID(remoteDevice0)
  627. if id != 0 {
  628. t.Errorf("index ID for remote device should default to zero, not %d", id)
  629. }
  630. // The Index ID for someone else should be settable
  631. s.SetIndexID(remoteDevice0, 42)
  632. id = s.IndexID(remoteDevice0)
  633. if id != 42 {
  634. t.Errorf("index ID for remote device should be remembered; got %d, expected %d", id, 42)
  635. }
  636. // Our own index ID should be generated randomly.
  637. id = s.IndexID(protocol.LocalDeviceID)
  638. if id == 0 {
  639. t.Errorf("index ID for local device should be random, not zero")
  640. }
  641. t.Logf("random index ID is 0x%016x", id)
  642. // But of course always the same after that.
  643. again := s.IndexID(protocol.LocalDeviceID)
  644. if again != id {
  645. t.Errorf("index ID changed; %d != %d", again, id)
  646. }
  647. }
  648. func TestDropFiles(t *testing.T) {
  649. ldb := db.OpenMemory()
  650. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  651. local0 := fileList{
  652. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  653. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  654. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  655. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  656. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  657. }
  658. remote0 := fileList{
  659. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  660. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  661. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  662. }
  663. // Insert files
  664. m.Update(protocol.LocalDeviceID, local0)
  665. m.Update(remoteDevice0, remote0)
  666. // Check that they're there
  667. h := haveList(m, protocol.LocalDeviceID)
  668. if len(h) != len(local0) {
  669. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  670. }
  671. h = haveList(m, remoteDevice0)
  672. if len(h) != len(remote0) {
  673. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  674. }
  675. g := globalList(m)
  676. if len(g) != len(local0) {
  677. // local0 covers all files
  678. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(local0))
  679. }
  680. // Drop the local files and recheck
  681. m.Drop(protocol.LocalDeviceID)
  682. h = haveList(m, protocol.LocalDeviceID)
  683. if len(h) != 0 {
  684. t.Errorf("Incorrect number of files after drop, %d != %d", len(h), 0)
  685. }
  686. h = haveList(m, remoteDevice0)
  687. if len(h) != len(remote0) {
  688. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  689. }
  690. g = globalList(m)
  691. if len(g) != len(remote0) {
  692. // the ones in remote0 remain
  693. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(remote0))
  694. }
  695. }
  696. func TestIssue4701(t *testing.T) {
  697. ldb := db.OpenMemory()
  698. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  699. localHave := fileList{
  700. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  701. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, LocalFlags: protocol.FlagLocalIgnored},
  702. }
  703. s.Update(protocol.LocalDeviceID, localHave)
  704. if c := s.LocalSize(); c.Files != 1 {
  705. t.Errorf("Expected 1 local file, got %v", c.Files)
  706. }
  707. if c := s.GlobalSize(); c.Files != 1 {
  708. t.Errorf("Expected 1 global file, got %v", c.Files)
  709. }
  710. localHave[1].LocalFlags = 0
  711. s.Update(protocol.LocalDeviceID, localHave)
  712. if c := s.LocalSize(); c.Files != 2 {
  713. t.Errorf("Expected 2 local files, got %v", c.Files)
  714. }
  715. if c := s.GlobalSize(); c.Files != 2 {
  716. t.Errorf("Expected 2 global files, got %v", c.Files)
  717. }
  718. localHave[0].LocalFlags = protocol.FlagLocalIgnored
  719. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  720. s.Update(protocol.LocalDeviceID, localHave)
  721. if c := s.LocalSize(); c.Files != 0 {
  722. t.Errorf("Expected 0 local files, got %v", c.Files)
  723. }
  724. if c := s.GlobalSize(); c.Files != 0 {
  725. t.Errorf("Expected 0 global files, got %v", c.Files)
  726. }
  727. }
  728. func TestWithHaveSequence(t *testing.T) {
  729. ldb := db.OpenMemory()
  730. folder := "test"
  731. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  732. // The files must not be in alphabetical order
  733. localHave := fileList{
  734. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, RawInvalid: true},
  735. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  736. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  737. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  738. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  739. }
  740. replace(s, protocol.LocalDeviceID, localHave)
  741. i := 2
  742. s.WithHaveSequence(int64(i), func(fi db.FileIntf) bool {
  743. if f := fi.(protocol.FileInfo); !f.IsEquivalent(localHave[i-1]) {
  744. t.Fatalf("Got %v\nExpected %v", f, localHave[i-1])
  745. }
  746. i++
  747. return true
  748. })
  749. }
  750. func TestStressWithHaveSequence(t *testing.T) {
  751. // This races two loops against each other: one that contiously does
  752. // updates, and one that continously does sequence walks. The test fails
  753. // if the sequence walker sees a discontinuity.
  754. if testing.Short() {
  755. t.Skip("Takes a long time")
  756. }
  757. ldb := db.OpenMemory()
  758. folder := "test"
  759. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  760. var localHave []protocol.FileInfo
  761. for i := 0; i < 100; i++ {
  762. localHave = append(localHave, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Blocks: genBlocks(i * 10)})
  763. }
  764. done := make(chan struct{})
  765. t0 := time.Now()
  766. go func() {
  767. for time.Since(t0) < 10*time.Second {
  768. for j, f := range localHave {
  769. localHave[j].Version = f.Version.Update(42)
  770. }
  771. s.Update(protocol.LocalDeviceID, localHave)
  772. }
  773. close(done)
  774. }()
  775. var prevSeq int64 = 0
  776. loop:
  777. for {
  778. select {
  779. case <-done:
  780. break loop
  781. default:
  782. }
  783. s.WithHaveSequence(prevSeq+1, func(fi db.FileIntf) bool {
  784. if fi.SequenceNo() < prevSeq+1 {
  785. t.Fatal("Skipped ", prevSeq+1, fi.SequenceNo())
  786. }
  787. prevSeq = fi.SequenceNo()
  788. return true
  789. })
  790. }
  791. }
  792. func TestIssue4925(t *testing.T) {
  793. ldb := db.OpenMemory()
  794. folder := "test"
  795. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  796. localHave := fileList{
  797. protocol.FileInfo{Name: "dir"},
  798. protocol.FileInfo{Name: "dir.file"},
  799. protocol.FileInfo{Name: "dir/file"},
  800. }
  801. replace(s, protocol.LocalDeviceID, localHave)
  802. for _, prefix := range []string{"dir", "dir/"} {
  803. pl := haveListPrefixed(s, protocol.LocalDeviceID, prefix)
  804. if l := len(pl); l != 2 {
  805. t.Errorf("Expected 2, got %v local items below %v", l, prefix)
  806. }
  807. pl = globalListPrefixed(s, prefix)
  808. if l := len(pl); l != 2 {
  809. t.Errorf("Expected 2, got %v global items below %v", l, prefix)
  810. }
  811. }
  812. }
  813. func TestMoveGlobalBack(t *testing.T) {
  814. ldb := db.OpenMemory()
  815. folder := "test"
  816. file := "foo"
  817. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  818. localHave := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Blocks: genBlocks(1), ModifiedS: 10, Size: 1}}
  819. 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}}
  820. s.Update(protocol.LocalDeviceID, localHave)
  821. s.Update(remoteDevice0, remote0Have)
  822. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  823. t.Error("Expected 1 local need, got", need)
  824. } else if !need[0].IsEquivalent(remote0Have[0]) {
  825. t.Errorf("Local need incorrect;\n A: %v !=\n E: %v", need[0], remote0Have[0])
  826. }
  827. if need := needList(s, remoteDevice0); len(need) != 0 {
  828. t.Error("Expected no need for remote 0, got", need)
  829. }
  830. ls := s.LocalSize()
  831. if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
  832. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  833. }
  834. gs := s.GlobalSize()
  835. if globalBytes := remote0Have[0].Size; gs.Bytes != globalBytes {
  836. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  837. }
  838. // That's what happens when something becomes unignored or something.
  839. // In any case it will be moved back from first spot in the global list
  840. // which is the scenario to be tested here.
  841. remote0Have[0].Version = remote0Have[0].Version.Update(remoteDevice0.Short()).DropOthers(remoteDevice0.Short())
  842. s.Update(remoteDevice0, remote0Have)
  843. if need := needList(s, remoteDevice0); len(need) != 1 {
  844. t.Error("Expected 1 need for remote 0, got", need)
  845. } else if !need[0].IsEquivalent(localHave[0]) {
  846. t.Errorf("Need for remote 0 incorrect;\n A: %v !=\n E: %v", need[0], localHave[0])
  847. }
  848. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  849. t.Error("Expected no local need, got", need)
  850. }
  851. ls = s.LocalSize()
  852. if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
  853. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  854. }
  855. gs = s.GlobalSize()
  856. if globalBytes := localHave[0].Size; gs.Bytes != globalBytes {
  857. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  858. }
  859. }
  860. // TestIssue5007 checks, that updating the local device with an invalid file
  861. // info with the newest version does indeed remove that file from the list of
  862. // needed files.
  863. // https://github.com/syncthing/syncthing/issues/5007
  864. func TestIssue5007(t *testing.T) {
  865. ldb := db.OpenMemory()
  866. folder := "test"
  867. file := "foo"
  868. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  869. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}}}
  870. s.Update(remoteDevice0, fs)
  871. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  872. t.Fatal("Expected 1 local need, got", need)
  873. } else if !need[0].IsEquivalent(fs[0]) {
  874. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  875. }
  876. fs[0].LocalFlags = protocol.FlagLocalIgnored
  877. s.Update(protocol.LocalDeviceID, fs)
  878. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  879. t.Fatal("Expected no local need, got", need)
  880. }
  881. }
  882. // TestNeedDeleted checks that a file that doesn't exist locally isn't needed
  883. // when the global file is deleted.
  884. func TestNeedDeleted(t *testing.T) {
  885. ldb := db.OpenMemory()
  886. folder := "test"
  887. file := "foo"
  888. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  889. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Deleted: true}}
  890. s.Update(remoteDevice0, fs)
  891. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  892. t.Fatal("Expected no local need, got", need)
  893. }
  894. fs[0].Deleted = false
  895. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  896. s.Update(remoteDevice0, fs)
  897. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  898. t.Fatal("Expected 1 local need, got", need)
  899. } else if !need[0].IsEquivalent(fs[0]) {
  900. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  901. }
  902. fs[0].Deleted = true
  903. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  904. s.Update(remoteDevice0, fs)
  905. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  906. t.Fatal("Expected no local need, got", need)
  907. }
  908. }
  909. func TestReceiveOnlyAccounting(t *testing.T) {
  910. ldb := db.OpenMemory()
  911. folder := "test"
  912. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  913. local := protocol.DeviceID{1}
  914. remote := protocol.DeviceID{2}
  915. // Three files that have been created by the remote device
  916. version := protocol.Vector{Counters: []protocol.Counter{{ID: remote.Short(), Value: 1}}}
  917. files := fileList{
  918. protocol.FileInfo{Name: "f1", Size: 10, Sequence: 1, Version: version},
  919. protocol.FileInfo{Name: "f2", Size: 10, Sequence: 1, Version: version},
  920. protocol.FileInfo{Name: "f3", Size: 10, Sequence: 1, Version: version},
  921. }
  922. // We have synced them locally
  923. replace(s, protocol.LocalDeviceID, files)
  924. replace(s, remote, files)
  925. if n := s.LocalSize().Files; n != 3 {
  926. t.Fatal("expected 3 local files initially, not", n)
  927. }
  928. if n := s.LocalSize().Bytes; n != 30 {
  929. t.Fatal("expected 30 local bytes initially, not", n)
  930. }
  931. if n := s.GlobalSize().Files; n != 3 {
  932. t.Fatal("expected 3 global files initially, not", n)
  933. }
  934. if n := s.GlobalSize().Bytes; n != 30 {
  935. t.Fatal("expected 30 global bytes initially, not", n)
  936. }
  937. if n := s.ReceiveOnlyChangedSize().Files; n != 0 {
  938. t.Fatal("expected 0 receive only changed files initially, not", n)
  939. }
  940. if n := s.ReceiveOnlyChangedSize().Bytes; n != 0 {
  941. t.Fatal("expected 0 receive only changed bytes initially, not", n)
  942. }
  943. // Detected a local change in a receive only folder
  944. changed := files[0]
  945. changed.Version = changed.Version.Update(local.Short())
  946. changed.Size = 100
  947. changed.ModifiedBy = local.Short()
  948. changed.LocalFlags = protocol.FlagLocalReceiveOnly
  949. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{changed})
  950. // Check that we see the files
  951. if n := s.LocalSize().Files; n != 3 {
  952. t.Fatal("expected 3 local files after local change, not", n)
  953. }
  954. if n := s.LocalSize().Bytes; n != 120 {
  955. t.Fatal("expected 120 local bytes after local change, not", n)
  956. }
  957. if n := s.GlobalSize().Files; n != 3 {
  958. t.Fatal("expected 3 global files after local change, not", n)
  959. }
  960. if n := s.GlobalSize().Bytes; n != 120 {
  961. t.Fatal("expected 120 global bytes after local change, not", n)
  962. }
  963. if n := s.ReceiveOnlyChangedSize().Files; n != 1 {
  964. t.Fatal("expected 1 receive only changed file after local change, not", n)
  965. }
  966. if n := s.ReceiveOnlyChangedSize().Bytes; n != 100 {
  967. t.Fatal("expected 100 receive only changed btyes after local change, not", n)
  968. }
  969. // Fake a revert. That's a two step process, first converting our
  970. // changed file into a less preferred variant, then pulling down the old
  971. // version.
  972. changed.Version = protocol.Vector{}
  973. changed.LocalFlags &^= protocol.FlagLocalReceiveOnly
  974. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{changed})
  975. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{files[0]})
  976. // Check that we see the files, same data as initially
  977. if n := s.LocalSize().Files; n != 3 {
  978. t.Fatal("expected 3 local files after revert, not", n)
  979. }
  980. if n := s.LocalSize().Bytes; n != 30 {
  981. t.Fatal("expected 30 local bytes after revert, not", n)
  982. }
  983. if n := s.GlobalSize().Files; n != 3 {
  984. t.Fatal("expected 3 global files after revert, not", n)
  985. }
  986. if n := s.GlobalSize().Bytes; n != 30 {
  987. t.Fatal("expected 30 global bytes after revert, not", n)
  988. }
  989. if n := s.ReceiveOnlyChangedSize().Files; n != 0 {
  990. t.Fatal("expected 0 receive only changed files after revert, not", n)
  991. }
  992. if n := s.ReceiveOnlyChangedSize().Bytes; n != 0 {
  993. t.Fatal("expected 0 receive only changed bytes after revert, not", n)
  994. }
  995. }
  996. func TestNeedAfterUnignore(t *testing.T) {
  997. ldb := db.OpenMemory()
  998. folder := "test"
  999. file := "foo"
  1000. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1001. remID := remoteDevice0.Short()
  1002. // Initial state: Devices in sync, locally ignored
  1003. local := protocol.FileInfo{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: remID, Value: 1}, {ID: myID, Value: 1}}}, ModifiedS: 10}
  1004. local.SetIgnored(myID)
  1005. remote := protocol.FileInfo{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: remID, Value: 1}, {ID: myID, Value: 1}}}, ModifiedS: 10}
  1006. s.Update(protocol.LocalDeviceID, fileList{local})
  1007. s.Update(remoteDevice0, fileList{remote})
  1008. // Unignore locally -> conflicting changes. Remote is newer, thus winning.
  1009. local.Version = local.Version.Update(myID)
  1010. local.Version = local.Version.DropOthers(myID)
  1011. local.LocalFlags = 0
  1012. local.ModifiedS = 0
  1013. s.Update(protocol.LocalDeviceID, fileList{local})
  1014. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  1015. t.Fatal("Expected one local need, got", need)
  1016. } else if !need[0].IsEquivalent(remote) {
  1017. t.Fatalf("Got %v, expected %v", need[0], remote)
  1018. }
  1019. }
  1020. func TestRemoteInvalidNotAccounted(t *testing.T) {
  1021. // Remote files with the invalid bit should not count.
  1022. ldb := db.OpenMemory()
  1023. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1024. files := []protocol.FileInfo{
  1025. {Name: "a", Size: 1234, Sequence: 42, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}}, // valid, should count
  1026. {Name: "b", Size: 1234, Sequence: 43, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, RawInvalid: true}, // invalid, doesn't count
  1027. }
  1028. s.Update(remoteDevice0, files)
  1029. global := s.GlobalSize()
  1030. if global.Files != 1 {
  1031. t.Error("Expected one file in global size, not", global.Files)
  1032. }
  1033. if global.Bytes != 1234 {
  1034. t.Error("Expected 1234 bytes in global size, not", global.Bytes)
  1035. }
  1036. }
  1037. func replace(fs *db.FileSet, device protocol.DeviceID, files []protocol.FileInfo) {
  1038. fs.Drop(device)
  1039. fs.Update(device, files)
  1040. }