set_test.go 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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), Invalid: 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), Invalid: true},
  303. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), Invalid: 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), Invalid: true},
  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}}}, Invalid: true},
  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].Invalid = true
  339. localHave[1].Blocks = nil
  340. localHave[4].Invalid = false
  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), Invalid: 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), Invalid: 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), Invalid: true},
  377. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), Invalid: 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}}}, Invalid: 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}}}, Invalid: true},
  532. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Invalid: 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}}}, Invalid: true},
  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].Invalid = false
  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].Invalid = true
  718. localHave[1].Invalid = true
  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}}}, Invalid: 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), Invalid: 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. func replace(fs *db.FileSet, device protocol.DeviceID, files []protocol.FileInfo) {
  818. fs.Drop(device)
  819. fs.Update(device, files)
  820. }