set_test.go 51 KB

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