set_test.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. "github.com/d4l3k/messagediff"
  14. "github.com/syncthing/syncthing/lib/db"
  15. "github.com/syncthing/syncthing/lib/fs"
  16. "github.com/syncthing/syncthing/lib/protocol"
  17. )
  18. var remoteDevice0, remoteDevice1 protocol.DeviceID
  19. func init() {
  20. remoteDevice0, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  21. remoteDevice1, _ = protocol.DeviceIDFromString("I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU")
  22. }
  23. const myID = 1
  24. func genBlocks(n int) []protocol.BlockInfo {
  25. b := make([]protocol.BlockInfo, n)
  26. for i := range b {
  27. h := make([]byte, 32)
  28. for j := range h {
  29. h[j] = byte(i + j)
  30. }
  31. b[i].Size = int32(i)
  32. b[i].Hash = h
  33. }
  34. return b
  35. }
  36. func globalList(s *db.FileSet) []protocol.FileInfo {
  37. var fs []protocol.FileInfo
  38. s.WithGlobal(func(fi db.FileIntf) bool {
  39. f := fi.(protocol.FileInfo)
  40. fs = append(fs, f)
  41. return true
  42. })
  43. return fs
  44. }
  45. func globalListPrefixed(s *db.FileSet, prefix string) []db.FileInfoTruncated {
  46. var fs []db.FileInfoTruncated
  47. s.WithPrefixedGlobalTruncated(prefix, func(fi db.FileIntf) bool {
  48. f := fi.(db.FileInfoTruncated)
  49. fs = append(fs, f)
  50. return true
  51. })
  52. return fs
  53. }
  54. func haveList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  55. var fs []protocol.FileInfo
  56. s.WithHave(n, func(fi db.FileIntf) bool {
  57. f := fi.(protocol.FileInfo)
  58. fs = append(fs, f)
  59. return true
  60. })
  61. return fs
  62. }
  63. func haveListPrefixed(s *db.FileSet, n protocol.DeviceID, prefix string) []db.FileInfoTruncated {
  64. var fs []db.FileInfoTruncated
  65. s.WithPrefixedHaveTruncated(n, prefix, func(fi db.FileIntf) bool {
  66. f := fi.(db.FileInfoTruncated)
  67. fs = append(fs, f)
  68. return true
  69. })
  70. return fs
  71. }
  72. func needList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  73. var fs []protocol.FileInfo
  74. s.WithNeed(n, func(fi db.FileIntf) bool {
  75. f := fi.(protocol.FileInfo)
  76. fs = append(fs, f)
  77. return true
  78. })
  79. return fs
  80. }
  81. type fileList []protocol.FileInfo
  82. func (l fileList) Len() int {
  83. return len(l)
  84. }
  85. func (l fileList) Less(a, b int) bool {
  86. return l[a].Name < l[b].Name
  87. }
  88. func (l fileList) Swap(a, b int) {
  89. l[a], l[b] = l[b], l[a]
  90. }
  91. func (l fileList) String() string {
  92. var b bytes.Buffer
  93. b.WriteString("[]protocol.FileList{\n")
  94. for _, f := range l {
  95. fmt.Fprintf(&b, " %q: #%v, %d bytes, %d blocks, perms=%o\n", f.Name, f.Version, f.Size, len(f.Blocks), f.Permissions)
  96. }
  97. b.WriteString("}")
  98. return b.String()
  99. }
  100. func TestGlobalSet(t *testing.T) {
  101. ldb := db.OpenMemory()
  102. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  103. local0 := fileList{
  104. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  105. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  106. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  107. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  108. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  109. }
  110. local1 := fileList{
  111. protocol.FileInfo{Name: "a", Sequence: 6, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  112. protocol.FileInfo{Name: "b", Sequence: 7, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  113. protocol.FileInfo{Name: "c", Sequence: 8, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  114. protocol.FileInfo{Name: "d", Sequence: 9, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  115. protocol.FileInfo{Name: "z", Sequence: 10, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  116. }
  117. localTot := fileList{
  118. local1[0],
  119. local1[1],
  120. local1[2],
  121. local1[3],
  122. protocol.FileInfo{Name: "z", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  123. }
  124. remote0 := fileList{
  125. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  126. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  127. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  128. }
  129. remote1 := fileList{
  130. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(6)},
  131. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(7)},
  132. }
  133. remoteTot := fileList{
  134. remote0[0],
  135. remote1[0],
  136. remote0[2],
  137. remote1[1],
  138. }
  139. expectedGlobal := fileList{
  140. remote0[0], // a
  141. remote1[0], // b
  142. remote0[2], // c
  143. localTot[3], // d
  144. remote1[1], // e
  145. localTot[4], // z
  146. }
  147. expectedLocalNeed := fileList{
  148. remote1[0],
  149. remote0[2],
  150. remote1[1],
  151. }
  152. expectedRemoteNeed := fileList{
  153. local0[3],
  154. }
  155. replace(m, protocol.LocalDeviceID, local0)
  156. replace(m, protocol.LocalDeviceID, local1)
  157. replace(m, remoteDevice0, remote0)
  158. m.Update(remoteDevice0, remote1)
  159. g := fileList(globalList(m))
  160. sort.Sort(g)
  161. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
  162. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal)
  163. }
  164. globalFiles, globalDirectories, globalDeleted, globalBytes := int32(0), int32(0), int32(0), int64(0)
  165. for _, f := range g {
  166. if f.IsInvalid() {
  167. continue
  168. }
  169. switch {
  170. case f.IsDeleted():
  171. globalDeleted++
  172. case f.IsDirectory():
  173. globalDirectories++
  174. default:
  175. globalFiles++
  176. }
  177. globalBytes += f.FileSize()
  178. }
  179. gs := m.GlobalSize()
  180. if gs.Files != globalFiles {
  181. t.Errorf("Incorrect GlobalSize files; %d != %d", gs.Files, globalFiles)
  182. }
  183. if gs.Directories != globalDirectories {
  184. t.Errorf("Incorrect GlobalSize directories; %d != %d", gs.Directories, globalDirectories)
  185. }
  186. if gs.Deleted != globalDeleted {
  187. t.Errorf("Incorrect GlobalSize deleted; %d != %d", gs.Deleted, globalDeleted)
  188. }
  189. if gs.Bytes != globalBytes {
  190. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  191. }
  192. h := fileList(haveList(m, protocol.LocalDeviceID))
  193. sort.Sort(h)
  194. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  195. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot)
  196. }
  197. haveFiles, haveDirectories, haveDeleted, haveBytes := int32(0), int32(0), int32(0), int64(0)
  198. for _, f := range h {
  199. if f.IsInvalid() {
  200. continue
  201. }
  202. switch {
  203. case f.IsDeleted():
  204. haveDeleted++
  205. case f.IsDirectory():
  206. haveDirectories++
  207. default:
  208. haveFiles++
  209. }
  210. haveBytes += f.FileSize()
  211. }
  212. ls := m.LocalSize()
  213. if ls.Files != haveFiles {
  214. t.Errorf("Incorrect LocalSize files; %d != %d", ls.Files, haveFiles)
  215. }
  216. if ls.Directories != haveDirectories {
  217. t.Errorf("Incorrect LocalSize directories; %d != %d", ls.Directories, haveDirectories)
  218. }
  219. if ls.Deleted != haveDeleted {
  220. t.Errorf("Incorrect LocalSize deleted; %d != %d", ls.Deleted, haveDeleted)
  221. }
  222. if ls.Bytes != haveBytes {
  223. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  224. }
  225. h = fileList(haveList(m, remoteDevice0))
  226. sort.Sort(h)
  227. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  228. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, remoteTot)
  229. }
  230. n := fileList(needList(m, protocol.LocalDeviceID))
  231. sort.Sort(n)
  232. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  233. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedLocalNeed)
  234. }
  235. n = fileList(needList(m, remoteDevice0))
  236. sort.Sort(n)
  237. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  238. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  239. }
  240. f, ok := m.Get(protocol.LocalDeviceID, "b")
  241. if !ok {
  242. t.Error("Unexpectedly not OK")
  243. }
  244. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  245. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  246. }
  247. f, ok = m.Get(remoteDevice0, "b")
  248. if !ok {
  249. t.Error("Unexpectedly not OK")
  250. }
  251. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  252. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  253. }
  254. f, ok = m.GetGlobal("b")
  255. if !ok {
  256. t.Error("Unexpectedly not OK")
  257. }
  258. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  259. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  260. }
  261. f, ok = m.Get(protocol.LocalDeviceID, "zz")
  262. if ok {
  263. t.Error("Unexpectedly OK")
  264. }
  265. if f.Name != "" {
  266. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  267. }
  268. f, ok = m.GetGlobal("zz")
  269. if ok {
  270. t.Error("Unexpectedly OK")
  271. }
  272. if f.Name != "" {
  273. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  274. }
  275. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  276. a := m.Availability("a")
  277. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  278. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  279. }
  280. a = m.Availability("b")
  281. if len(a) != 1 || a[0] != remoteDevice0 {
  282. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  283. }
  284. a = m.Availability("d")
  285. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  286. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  287. }
  288. }
  289. func TestNeedWithInvalid(t *testing.T) {
  290. ldb := db.OpenMemory()
  291. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  292. localHave := fileList{
  293. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  294. }
  295. remote0Have := fileList{
  296. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  297. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  298. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  299. }
  300. remote1Have := fileList{
  301. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  302. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  303. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  304. }
  305. expectedNeed := fileList{
  306. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  307. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  308. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  309. }
  310. replace(s, protocol.LocalDeviceID, localHave)
  311. replace(s, remoteDevice0, remote0Have)
  312. replace(s, remoteDevice1, remote1Have)
  313. need := fileList(needList(s, protocol.LocalDeviceID))
  314. sort.Sort(need)
  315. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  316. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  317. }
  318. }
  319. func TestUpdateToInvalid(t *testing.T) {
  320. ldb := db.OpenMemory()
  321. folder := "test"
  322. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  323. f := db.NewBlockFinder(ldb)
  324. localHave := fileList{
  325. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  326. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  327. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), LocalFlags: protocol.FlagLocalIgnored},
  328. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  329. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, LocalFlags: protocol.FlagLocalIgnored},
  330. }
  331. replace(s, protocol.LocalDeviceID, localHave)
  332. have := fileList(haveList(s, protocol.LocalDeviceID))
  333. sort.Sort(have)
  334. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  335. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  336. }
  337. oldBlockHash := localHave[1].Blocks[0].Hash
  338. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  339. localHave[1].Blocks = nil
  340. localHave[4].LocalFlags = 0
  341. localHave[4].Blocks = genBlocks(3)
  342. s.Update(protocol.LocalDeviceID, append(fileList{}, localHave[1], localHave[4]))
  343. have = fileList(haveList(s, protocol.LocalDeviceID))
  344. sort.Sort(have)
  345. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  346. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  347. }
  348. f.Iterate([]string{folder}, oldBlockHash, func(folder, file string, index int32) bool {
  349. if file == localHave[1].Name {
  350. t.Errorf("Found unexpected block in blockmap for invalidated file")
  351. return true
  352. }
  353. return false
  354. })
  355. if !f.Iterate([]string{folder}, localHave[4].Blocks[0].Hash, func(folder, file string, index int32) bool {
  356. if file == localHave[4].Name {
  357. return true
  358. }
  359. return false
  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.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.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.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.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.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.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.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: string(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 TestCommitted(t *testing.T) {
  574. // Verify that the Committed counter increases when we change things and
  575. // doesn't increase when we don't.
  576. ldb := db.OpenMemory()
  577. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  578. local := []protocol.FileInfo{
  579. {Name: string("file"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  580. }
  581. // Adding a file should increase the counter
  582. c0 := ldb.Committed()
  583. replace(s, protocol.LocalDeviceID, local)
  584. c1 := ldb.Committed()
  585. if c1 <= c0 {
  586. t.Errorf("committed data didn't increase; %d <= %d", c1, c0)
  587. }
  588. // Updating with something identical should not do anything
  589. s.Update(protocol.LocalDeviceID, local)
  590. c2 := ldb.Committed()
  591. if c2 > c1 {
  592. t.Errorf("replace with same contents should do nothing but %d > %d", c2, c1)
  593. }
  594. }
  595. func BenchmarkUpdateOneFile(b *testing.B) {
  596. local0 := fileList{
  597. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  598. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  599. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  600. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  601. // A longer name is more realistic and causes more allocations
  602. protocol.FileInfo{Name: "zajksdhaskjdh/askjdhaskjdashkajshd/kasjdhaskjdhaskdjhaskdjash/dkjashdaksjdhaskdjahskdjh", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  603. }
  604. ldb, err := db.Open("testdata/benchmarkupdate.db")
  605. if err != nil {
  606. b.Fatal(err)
  607. }
  608. defer func() {
  609. ldb.Close()
  610. os.RemoveAll("testdata/benchmarkupdate.db")
  611. }()
  612. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  613. replace(m, protocol.LocalDeviceID, local0)
  614. l := local0[4:5]
  615. for i := 0; i < b.N; i++ {
  616. l[0].Version = l[0].Version.Update(myID)
  617. m.Update(protocol.LocalDeviceID, local0)
  618. }
  619. b.ReportAllocs()
  620. }
  621. func TestIndexID(t *testing.T) {
  622. ldb := db.OpenMemory()
  623. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  624. // The Index ID for some random device is zero by default.
  625. id := s.IndexID(remoteDevice0)
  626. if id != 0 {
  627. t.Errorf("index ID for remote device should default to zero, not %d", id)
  628. }
  629. // The Index ID for someone else should be settable
  630. s.SetIndexID(remoteDevice0, 42)
  631. id = s.IndexID(remoteDevice0)
  632. if id != 42 {
  633. t.Errorf("index ID for remote device should be remembered; got %d, expected %d", id, 42)
  634. }
  635. // Our own index ID should be generated randomly.
  636. id = s.IndexID(protocol.LocalDeviceID)
  637. if id == 0 {
  638. t.Errorf("index ID for local device should be random, not zero")
  639. }
  640. t.Logf("random index ID is 0x%016x", id)
  641. // But of course always the same after that.
  642. again := s.IndexID(protocol.LocalDeviceID)
  643. if again != id {
  644. t.Errorf("index ID changed; %d != %d", again, id)
  645. }
  646. }
  647. func TestDropFiles(t *testing.T) {
  648. ldb := db.OpenMemory()
  649. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  650. local0 := fileList{
  651. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  652. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  653. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  654. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  655. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  656. }
  657. remote0 := fileList{
  658. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  659. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  660. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  661. }
  662. // Insert files
  663. m.Update(protocol.LocalDeviceID, local0)
  664. m.Update(remoteDevice0, remote0)
  665. // Check that they're there
  666. h := haveList(m, protocol.LocalDeviceID)
  667. if len(h) != len(local0) {
  668. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  669. }
  670. h = haveList(m, remoteDevice0)
  671. if len(h) != len(remote0) {
  672. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  673. }
  674. g := globalList(m)
  675. if len(g) != len(local0) {
  676. // local0 covers all files
  677. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(local0))
  678. }
  679. // Drop the local files and recheck
  680. m.Drop(protocol.LocalDeviceID)
  681. h = haveList(m, protocol.LocalDeviceID)
  682. if len(h) != 0 {
  683. t.Errorf("Incorrect number of files after drop, %d != %d", len(h), 0)
  684. }
  685. h = haveList(m, remoteDevice0)
  686. if len(h) != len(remote0) {
  687. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  688. }
  689. g = globalList(m)
  690. if len(g) != len(remote0) {
  691. // the ones in remote0 remain
  692. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(remote0))
  693. }
  694. }
  695. func TestIssue4701(t *testing.T) {
  696. ldb := db.OpenMemory()
  697. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  698. localHave := fileList{
  699. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  700. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, LocalFlags: protocol.FlagLocalIgnored},
  701. }
  702. s.Update(protocol.LocalDeviceID, localHave)
  703. if c := s.LocalSize(); c.Files != 1 {
  704. t.Errorf("Expected 1 local file, got %v", c.Files)
  705. }
  706. if c := s.GlobalSize(); c.Files != 1 {
  707. t.Errorf("Expected 1 global file, got %v", c.Files)
  708. }
  709. localHave[1].LocalFlags = 0
  710. s.Update(protocol.LocalDeviceID, localHave)
  711. if c := s.LocalSize(); c.Files != 2 {
  712. t.Errorf("Expected 2 local files, got %v", c.Files)
  713. }
  714. if c := s.GlobalSize(); c.Files != 2 {
  715. t.Errorf("Expected 2 global files, got %v", c.Files)
  716. }
  717. localHave[0].LocalFlags = protocol.FlagLocalIgnored
  718. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  719. s.Update(protocol.LocalDeviceID, localHave)
  720. if c := s.LocalSize(); c.Files != 0 {
  721. t.Errorf("Expected 0 local files, got %v", c.Files)
  722. }
  723. if c := s.GlobalSize(); c.Files != 0 {
  724. t.Errorf("Expected 0 global files, got %v", c.Files)
  725. }
  726. }
  727. func TestWithHaveSequence(t *testing.T) {
  728. ldb := db.OpenMemory()
  729. folder := "test"
  730. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  731. // The files must not be in alphabetical order
  732. localHave := fileList{
  733. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, RawInvalid: true},
  734. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  735. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  736. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  737. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  738. }
  739. replace(s, protocol.LocalDeviceID, localHave)
  740. i := 2
  741. s.WithHaveSequence(int64(i), func(fi db.FileIntf) bool {
  742. if f := fi.(protocol.FileInfo); !f.IsEquivalent(localHave[i-1], false, false) {
  743. t.Fatalf("Got %v\nExpected %v", f, localHave[i-1])
  744. }
  745. i++
  746. return true
  747. })
  748. }
  749. func TestIssue4925(t *testing.T) {
  750. ldb := db.OpenMemory()
  751. folder := "test)"
  752. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  753. localHave := fileList{
  754. protocol.FileInfo{Name: "dir"},
  755. protocol.FileInfo{Name: "dir.file"},
  756. protocol.FileInfo{Name: "dir/file"},
  757. }
  758. replace(s, protocol.LocalDeviceID, localHave)
  759. for _, prefix := range []string{"dir", "dir/"} {
  760. pl := haveListPrefixed(s, protocol.LocalDeviceID, prefix)
  761. if l := len(pl); l != 2 {
  762. t.Errorf("Expected 2, got %v local items below %v", l, prefix)
  763. }
  764. pl = globalListPrefixed(s, prefix)
  765. if l := len(pl); l != 2 {
  766. t.Errorf("Expected 2, got %v global items below %v", l, prefix)
  767. }
  768. }
  769. }
  770. func TestMoveGlobalBack(t *testing.T) {
  771. ldb := db.OpenMemory()
  772. folder := "test"
  773. file := "foo"
  774. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  775. localHave := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Blocks: genBlocks(1), ModifiedS: 10, Size: 1}}
  776. 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}}
  777. s.Update(protocol.LocalDeviceID, localHave)
  778. s.Update(remoteDevice0, remote0Have)
  779. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  780. t.Error("Expected 1 local need, got", need)
  781. } else if !need[0].IsEquivalent(remote0Have[0], false, false) {
  782. t.Errorf("Local need incorrect;\n A: %v !=\n E: %v", need[0], remote0Have[0])
  783. }
  784. if need := needList(s, remoteDevice0); len(need) != 0 {
  785. t.Error("Expected no need for remote 0, got", need)
  786. }
  787. ls := s.LocalSize()
  788. if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
  789. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  790. }
  791. gs := s.GlobalSize()
  792. if globalBytes := remote0Have[0].Size; gs.Bytes != globalBytes {
  793. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  794. }
  795. // That's what happens when something becomes unignored or something.
  796. // In any case it will be moved back from first spot in the global list
  797. // which is the scenario to be tested here.
  798. remote0Have[0].Version = remote0Have[0].Version.Update(remoteDevice0.Short()).DropOthers(remoteDevice0.Short())
  799. s.Update(remoteDevice0, remote0Have)
  800. if need := needList(s, remoteDevice0); len(need) != 1 {
  801. t.Error("Expected 1 need for remote 0, got", need)
  802. } else if !need[0].IsEquivalent(localHave[0], false, false) {
  803. t.Errorf("Need for remote 0 incorrect;\n A: %v !=\n E: %v", need[0], localHave[0])
  804. }
  805. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  806. t.Error("Expected no local need, 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 := localHave[0].Size; gs.Bytes != globalBytes {
  814. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  815. }
  816. }
  817. // TestIssue5007 checks, that updating the local device with an invalid file
  818. // info with the newest version does indeed remove that file from the list of
  819. // needed files.
  820. // https://github.com/syncthing/syncthing/issues/5007
  821. func TestIssue5007(t *testing.T) {
  822. ldb := db.OpenMemory()
  823. folder := "test"
  824. file := "foo"
  825. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  826. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}}}
  827. s.Update(remoteDevice0, fs)
  828. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  829. t.Fatal("Expected 1 local need, got", need)
  830. } else if !need[0].IsEquivalent(fs[0], false, false) {
  831. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  832. }
  833. fs[0].LocalFlags = protocol.FlagLocalIgnored
  834. s.Update(protocol.LocalDeviceID, fs)
  835. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  836. t.Fatal("Expected no local need, got", need)
  837. }
  838. }
  839. // TestNeedDeleted checks that a file that doesn't exist locally isn't needed
  840. // when the global file is deleted.
  841. func TestNeedDeleted(t *testing.T) {
  842. ldb := db.OpenMemory()
  843. folder := "test"
  844. file := "foo"
  845. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  846. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Deleted: true}}
  847. s.Update(remoteDevice0, fs)
  848. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  849. t.Fatal("Expected no local need, got", need)
  850. }
  851. fs[0].Deleted = false
  852. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  853. s.Update(remoteDevice0, fs)
  854. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  855. t.Fatal("Expected 1 local need, got", need)
  856. } else if !need[0].IsEquivalent(fs[0], false, false) {
  857. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  858. }
  859. fs[0].Deleted = true
  860. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  861. s.Update(remoteDevice0, fs)
  862. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  863. t.Fatal("Expected no local need, got", need)
  864. }
  865. }
  866. func replace(fs *db.FileSet, device protocol.DeviceID, files []protocol.FileInfo) {
  867. fs.Drop(device)
  868. fs.Update(device, files)
  869. }