set_test.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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 haveList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  46. var fs []protocol.FileInfo
  47. s.WithHave(n, func(fi db.FileIntf) bool {
  48. f := fi.(protocol.FileInfo)
  49. fs = append(fs, f)
  50. return true
  51. })
  52. return fs
  53. }
  54. func needList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  55. var fs []protocol.FileInfo
  56. s.WithNeed(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. type fileList []protocol.FileInfo
  64. func (l fileList) Len() int {
  65. return len(l)
  66. }
  67. func (l fileList) Less(a, b int) bool {
  68. return l[a].Name < l[b].Name
  69. }
  70. func (l fileList) Swap(a, b int) {
  71. l[a], l[b] = l[b], l[a]
  72. }
  73. func (l fileList) String() string {
  74. var b bytes.Buffer
  75. b.WriteString("[]protocol.FileList{\n")
  76. for _, f := range l {
  77. fmt.Fprintf(&b, " %q: #%v, %d bytes, %d blocks, perms=%o\n", f.Name, f.Version, f.Size, len(f.Blocks), f.Permissions)
  78. }
  79. b.WriteString("}")
  80. return b.String()
  81. }
  82. func TestGlobalSet(t *testing.T) {
  83. ldb := db.OpenMemory()
  84. m := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  85. local0 := fileList{
  86. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  87. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  88. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  89. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  90. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  91. }
  92. local1 := fileList{
  93. protocol.FileInfo{Name: "a", Sequence: 6, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  94. protocol.FileInfo{Name: "b", Sequence: 7, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  95. protocol.FileInfo{Name: "c", Sequence: 8, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  96. protocol.FileInfo{Name: "d", Sequence: 9, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  97. protocol.FileInfo{Name: "z", Sequence: 10, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  98. }
  99. localTot := fileList{
  100. local1[0],
  101. local1[1],
  102. local1[2],
  103. local1[3],
  104. protocol.FileInfo{Name: "z", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  105. }
  106. remote0 := fileList{
  107. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  108. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  109. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  110. }
  111. remote1 := fileList{
  112. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(6)},
  113. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(7)},
  114. }
  115. remoteTot := fileList{
  116. remote0[0],
  117. remote1[0],
  118. remote0[2],
  119. remote1[1],
  120. }
  121. expectedGlobal := fileList{
  122. remote0[0], // a
  123. remote1[0], // b
  124. remote0[2], // c
  125. localTot[3], // d
  126. remote1[1], // e
  127. localTot[4], // z
  128. }
  129. expectedLocalNeed := fileList{
  130. remote1[0],
  131. remote0[2],
  132. remote1[1],
  133. }
  134. expectedRemoteNeed := fileList{
  135. local0[3],
  136. }
  137. replace(m, protocol.LocalDeviceID, local0)
  138. replace(m, protocol.LocalDeviceID, local1)
  139. replace(m, remoteDevice0, remote0)
  140. m.Update(remoteDevice0, remote1)
  141. g := fileList(globalList(m))
  142. sort.Sort(g)
  143. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
  144. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal)
  145. }
  146. globalFiles, globalDirectories, globalDeleted, globalBytes := int32(0), int32(0), int32(0), int64(0)
  147. for _, f := range g {
  148. if f.IsInvalid() {
  149. continue
  150. }
  151. switch {
  152. case f.IsDeleted():
  153. globalDeleted++
  154. case f.IsDirectory():
  155. globalDirectories++
  156. default:
  157. globalFiles++
  158. }
  159. globalBytes += f.FileSize()
  160. }
  161. gs := m.GlobalSize()
  162. if gs.Files != globalFiles {
  163. t.Errorf("Incorrect GlobalSize files; %d != %d", gs.Files, globalFiles)
  164. }
  165. if gs.Directories != globalDirectories {
  166. t.Errorf("Incorrect GlobalSize directories; %d != %d", gs.Directories, globalDirectories)
  167. }
  168. if gs.Deleted != globalDeleted {
  169. t.Errorf("Incorrect GlobalSize deleted; %d != %d", gs.Deleted, globalDeleted)
  170. }
  171. if gs.Bytes != globalBytes {
  172. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  173. }
  174. h := fileList(haveList(m, protocol.LocalDeviceID))
  175. sort.Sort(h)
  176. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  177. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot)
  178. }
  179. haveFiles, haveDirectories, haveDeleted, haveBytes := int32(0), int32(0), int32(0), int64(0)
  180. for _, f := range h {
  181. if f.IsInvalid() {
  182. continue
  183. }
  184. switch {
  185. case f.IsDeleted():
  186. haveDeleted++
  187. case f.IsDirectory():
  188. haveDirectories++
  189. default:
  190. haveFiles++
  191. }
  192. haveBytes += f.FileSize()
  193. }
  194. ls := m.LocalSize()
  195. if ls.Files != haveFiles {
  196. t.Errorf("Incorrect LocalSize files; %d != %d", ls.Files, haveFiles)
  197. }
  198. if ls.Directories != haveDirectories {
  199. t.Errorf("Incorrect LocalSize directories; %d != %d", ls.Directories, haveDirectories)
  200. }
  201. if ls.Deleted != haveDeleted {
  202. t.Errorf("Incorrect LocalSize deleted; %d != %d", ls.Deleted, haveDeleted)
  203. }
  204. if ls.Bytes != haveBytes {
  205. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  206. }
  207. h = fileList(haveList(m, remoteDevice0))
  208. sort.Sort(h)
  209. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  210. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, remoteTot)
  211. }
  212. n := fileList(needList(m, protocol.LocalDeviceID))
  213. sort.Sort(n)
  214. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  215. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedLocalNeed)
  216. }
  217. n = fileList(needList(m, remoteDevice0))
  218. sort.Sort(n)
  219. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  220. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  221. }
  222. f, ok := m.Get(protocol.LocalDeviceID, "b")
  223. if !ok {
  224. t.Error("Unexpectedly not OK")
  225. }
  226. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  227. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  228. }
  229. f, ok = m.Get(remoteDevice0, "b")
  230. if !ok {
  231. t.Error("Unexpectedly not OK")
  232. }
  233. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  234. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  235. }
  236. f, ok = m.GetGlobal("b")
  237. if !ok {
  238. t.Error("Unexpectedly not OK")
  239. }
  240. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  241. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  242. }
  243. f, ok = m.Get(protocol.LocalDeviceID, "zz")
  244. if ok {
  245. t.Error("Unexpectedly OK")
  246. }
  247. if f.Name != "" {
  248. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  249. }
  250. f, ok = m.GetGlobal("zz")
  251. if ok {
  252. t.Error("Unexpectedly OK")
  253. }
  254. if f.Name != "" {
  255. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  256. }
  257. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  258. a := m.Availability("a")
  259. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  260. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  261. }
  262. a = m.Availability("b")
  263. if len(a) != 1 || a[0] != remoteDevice0 {
  264. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  265. }
  266. a = m.Availability("d")
  267. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  268. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  269. }
  270. }
  271. func TestNeedWithInvalid(t *testing.T) {
  272. ldb := db.OpenMemory()
  273. s := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  274. localHave := fileList{
  275. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  276. }
  277. remote0Have := fileList{
  278. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  279. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), Invalid: true},
  280. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  281. }
  282. remote1Have := fileList{
  283. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  284. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), Invalid: true},
  285. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), Invalid: true},
  286. }
  287. expectedNeed := fileList{
  288. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  289. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  290. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  291. }
  292. replace(s, protocol.LocalDeviceID, localHave)
  293. replace(s, remoteDevice0, remote0Have)
  294. replace(s, remoteDevice1, remote1Have)
  295. need := fileList(needList(s, protocol.LocalDeviceID))
  296. sort.Sort(need)
  297. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  298. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  299. }
  300. }
  301. func TestUpdateToInvalid(t *testing.T) {
  302. ldb := db.OpenMemory()
  303. folder := "test)"
  304. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  305. f := db.NewBlockFinder(ldb)
  306. localHave := fileList{
  307. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  308. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  309. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), Invalid: true},
  310. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  311. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Invalid: true},
  312. }
  313. replace(s, protocol.LocalDeviceID, localHave)
  314. have := fileList(haveList(s, protocol.LocalDeviceID))
  315. sort.Sort(have)
  316. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  317. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  318. }
  319. oldBlockHash := localHave[1].Blocks[0].Hash
  320. localHave[1].Invalid = true
  321. localHave[1].Blocks = nil
  322. localHave[4].Invalid = false
  323. localHave[4].Blocks = genBlocks(3)
  324. s.Update(protocol.LocalDeviceID, append(fileList{}, localHave[1], localHave[4]))
  325. have = fileList(haveList(s, protocol.LocalDeviceID))
  326. sort.Sort(have)
  327. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  328. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  329. }
  330. f.Iterate([]string{folder}, oldBlockHash, func(folder, file string, index int32) bool {
  331. if file == localHave[1].Name {
  332. t.Errorf("Found unexpected block in blockmap for invalidated file")
  333. return true
  334. }
  335. return false
  336. })
  337. if !f.Iterate([]string{folder}, localHave[4].Blocks[0].Hash, func(folder, file string, index int32) bool {
  338. if file == localHave[4].Name {
  339. return true
  340. }
  341. return false
  342. }) {
  343. t.Errorf("First block of un-invalidated file is missing from blockmap")
  344. }
  345. }
  346. func TestInvalidAvailability(t *testing.T) {
  347. ldb := db.OpenMemory()
  348. s := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  349. remote0Have := fileList{
  350. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  351. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), Invalid: true},
  352. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  353. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), Invalid: true},
  354. }
  355. remote1Have := fileList{
  356. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  357. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  358. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), Invalid: true},
  359. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), Invalid: true},
  360. }
  361. replace(s, remoteDevice0, remote0Have)
  362. replace(s, remoteDevice1, remote1Have)
  363. if av := s.Availability("both"); len(av) != 2 {
  364. t.Error("Incorrect availability for 'both':", av)
  365. }
  366. if av := s.Availability("r0only"); len(av) != 1 || av[0] != remoteDevice0 {
  367. t.Error("Incorrect availability for 'r0only':", av)
  368. }
  369. if av := s.Availability("r1only"); len(av) != 1 || av[0] != remoteDevice1 {
  370. t.Error("Incorrect availability for 'r1only':", av)
  371. }
  372. if av := s.Availability("none"); len(av) != 0 {
  373. t.Error("Incorrect availability for 'none':", av)
  374. }
  375. }
  376. func TestGlobalReset(t *testing.T) {
  377. ldb := db.OpenMemory()
  378. m := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  379. local := []protocol.FileInfo{
  380. {Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  381. {Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  382. {Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  383. {Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  384. }
  385. remote := []protocol.FileInfo{
  386. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  387. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  388. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  389. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  390. }
  391. replace(m, protocol.LocalDeviceID, local)
  392. g := globalList(m)
  393. sort.Sort(fileList(g))
  394. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  395. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  396. }
  397. replace(m, remoteDevice0, remote)
  398. replace(m, remoteDevice0, nil)
  399. g = globalList(m)
  400. sort.Sort(fileList(g))
  401. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  402. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  403. }
  404. }
  405. func TestNeed(t *testing.T) {
  406. ldb := db.OpenMemory()
  407. m := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  408. local := []protocol.FileInfo{
  409. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  410. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  411. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  412. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  413. }
  414. remote := []protocol.FileInfo{
  415. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  416. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  417. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  418. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  419. }
  420. shouldNeed := []protocol.FileInfo{
  421. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  422. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  423. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  424. }
  425. replace(m, protocol.LocalDeviceID, local)
  426. replace(m, remoteDevice0, remote)
  427. need := needList(m, protocol.LocalDeviceID)
  428. sort.Sort(fileList(need))
  429. sort.Sort(fileList(shouldNeed))
  430. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  431. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  432. }
  433. }
  434. func TestSequence(t *testing.T) {
  435. ldb := db.OpenMemory()
  436. m := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  437. local1 := []protocol.FileInfo{
  438. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  439. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  440. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  441. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  442. }
  443. local2 := []protocol.FileInfo{
  444. local1[0],
  445. // [1] deleted
  446. local1[2],
  447. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  448. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  449. }
  450. replace(m, protocol.LocalDeviceID, local1)
  451. c0 := m.Sequence(protocol.LocalDeviceID)
  452. replace(m, protocol.LocalDeviceID, local2)
  453. c1 := m.Sequence(protocol.LocalDeviceID)
  454. if !(c1 > c0) {
  455. t.Fatal("Local version number should have incremented")
  456. }
  457. }
  458. func TestListDropFolder(t *testing.T) {
  459. ldb := db.OpenMemory()
  460. s0 := db.NewFileSet("test0", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  461. local1 := []protocol.FileInfo{
  462. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  463. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  464. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  465. }
  466. replace(s0, protocol.LocalDeviceID, local1)
  467. s1 := db.NewFileSet("test1", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  468. local2 := []protocol.FileInfo{
  469. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  470. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  471. {Name: "f", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  472. }
  473. replace(s1, remoteDevice0, local2)
  474. // Check that we have both folders and their data is in the global list
  475. expectedFolderList := []string{"test0", "test1"}
  476. actualFolderList := ldb.ListFolders()
  477. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  478. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  479. }
  480. if l := len(globalList(s0)); l != 3 {
  481. t.Errorf("Incorrect global length %d != 3 for s0", l)
  482. }
  483. if l := len(globalList(s1)); l != 3 {
  484. t.Errorf("Incorrect global length %d != 3 for s1", l)
  485. }
  486. // Drop one of them and check that it's gone.
  487. db.DropFolder(ldb, "test1")
  488. expectedFolderList = []string{"test0"}
  489. actualFolderList = ldb.ListFolders()
  490. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  491. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  492. }
  493. if l := len(globalList(s0)); l != 3 {
  494. t.Errorf("Incorrect global length %d != 3 for s0", l)
  495. }
  496. if l := len(globalList(s1)); l != 0 {
  497. t.Errorf("Incorrect global length %d != 0 for s1", l)
  498. }
  499. }
  500. func TestGlobalNeedWithInvalid(t *testing.T) {
  501. ldb := db.OpenMemory()
  502. s := db.NewFileSet("test1", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  503. rem0 := fileList{
  504. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  505. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Invalid: true},
  506. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  507. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  508. }
  509. replace(s, remoteDevice0, rem0)
  510. rem1 := fileList{
  511. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  512. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  513. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Invalid: true},
  514. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Invalid: true, ModifiedS: 10},
  515. }
  516. replace(s, remoteDevice1, rem1)
  517. total := fileList{
  518. // There's a valid copy of each file, so it should be merged
  519. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  520. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  521. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  522. // in conflict and older, but still wins as the other is invalid
  523. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  524. }
  525. need := fileList(needList(s, protocol.LocalDeviceID))
  526. if fmt.Sprint(need) != fmt.Sprint(total) {
  527. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
  528. }
  529. global := fileList(globalList(s))
  530. if fmt.Sprint(global) != fmt.Sprint(total) {
  531. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
  532. }
  533. }
  534. func TestLongPath(t *testing.T) {
  535. ldb := db.OpenMemory()
  536. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  537. var b bytes.Buffer
  538. for i := 0; i < 100; i++ {
  539. b.WriteString("012345678901234567890123456789012345678901234567890")
  540. }
  541. name := b.String() // 5000 characters
  542. local := []protocol.FileInfo{
  543. {Name: string(name), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  544. }
  545. replace(s, protocol.LocalDeviceID, local)
  546. gf := globalList(s)
  547. if l := len(gf); l != 1 {
  548. t.Fatalf("Incorrect len %d != 1 for global list", l)
  549. }
  550. if gf[0].Name != local[0].Name {
  551. t.Errorf("Incorrect long filename;\n%q !=\n%q",
  552. gf[0].Name, local[0].Name)
  553. }
  554. }
  555. func TestCommitted(t *testing.T) {
  556. // Verify that the Committed counter increases when we change things and
  557. // doesn't increase when we don't.
  558. ldb := db.OpenMemory()
  559. s := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  560. local := []protocol.FileInfo{
  561. {Name: string("file"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  562. }
  563. // Adding a file should increase the counter
  564. c0 := ldb.Committed()
  565. replace(s, protocol.LocalDeviceID, local)
  566. c1 := ldb.Committed()
  567. if c1 <= c0 {
  568. t.Errorf("committed data didn't increase; %d <= %d", c1, c0)
  569. }
  570. // Updating with something identical should not do anything
  571. s.Update(protocol.LocalDeviceID, local)
  572. c2 := ldb.Committed()
  573. if c2 > c1 {
  574. t.Errorf("replace with same contents should do nothing but %d > %d", c2, c1)
  575. }
  576. }
  577. func BenchmarkUpdateOneFile(b *testing.B) {
  578. local0 := fileList{
  579. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  580. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  581. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  582. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  583. // A longer name is more realistic and causes more allocations
  584. protocol.FileInfo{Name: "zajksdhaskjdh/askjdhaskjdashkajshd/kasjdhaskjdhaskdjhaskdjash/dkjashdaksjdhaskdjahskdjh", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  585. }
  586. ldb, err := db.Open("testdata/benchmarkupdate.db")
  587. if err != nil {
  588. b.Fatal(err)
  589. }
  590. defer func() {
  591. ldb.Close()
  592. os.RemoveAll("testdata/benchmarkupdate.db")
  593. }()
  594. m := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  595. replace(m, protocol.LocalDeviceID, local0)
  596. l := local0[4:5]
  597. for i := 0; i < b.N; i++ {
  598. l[0].Version = l[0].Version.Update(myID)
  599. m.Update(protocol.LocalDeviceID, local0)
  600. }
  601. b.ReportAllocs()
  602. }
  603. func TestIndexID(t *testing.T) {
  604. ldb := db.OpenMemory()
  605. s := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  606. // The Index ID for some random device is zero by default.
  607. id := s.IndexID(remoteDevice0)
  608. if id != 0 {
  609. t.Errorf("index ID for remote device should default to zero, not %d", id)
  610. }
  611. // The Index ID for someone else should be settable
  612. s.SetIndexID(remoteDevice0, 42)
  613. id = s.IndexID(remoteDevice0)
  614. if id != 42 {
  615. t.Errorf("index ID for remote device should be remembered; got %d, expected %d", id, 42)
  616. }
  617. // Our own index ID should be generated randomly.
  618. id = s.IndexID(protocol.LocalDeviceID)
  619. if id == 0 {
  620. t.Errorf("index ID for local device should be random, not zero")
  621. }
  622. t.Logf("random index ID is 0x%016x", id)
  623. // But of course always the same after that.
  624. again := s.IndexID(protocol.LocalDeviceID)
  625. if again != id {
  626. t.Errorf("index ID changed; %d != %d", again, id)
  627. }
  628. }
  629. func TestDropFiles(t *testing.T) {
  630. ldb := db.OpenMemory()
  631. m := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  632. local0 := fileList{
  633. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  634. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  635. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  636. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  637. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  638. }
  639. remote0 := fileList{
  640. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  641. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  642. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  643. }
  644. // Insert files
  645. m.Update(protocol.LocalDeviceID, local0)
  646. m.Update(remoteDevice0, remote0)
  647. // Check that they're there
  648. h := haveList(m, protocol.LocalDeviceID)
  649. if len(h) != len(local0) {
  650. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  651. }
  652. h = haveList(m, remoteDevice0)
  653. if len(h) != len(remote0) {
  654. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  655. }
  656. g := globalList(m)
  657. if len(g) != len(local0) {
  658. // local0 covers all files
  659. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(local0))
  660. }
  661. // Drop the local files and recheck
  662. m.Drop(protocol.LocalDeviceID)
  663. h = haveList(m, protocol.LocalDeviceID)
  664. if len(h) != 0 {
  665. t.Errorf("Incorrect number of files after drop, %d != %d", len(h), 0)
  666. }
  667. h = haveList(m, remoteDevice0)
  668. if len(h) != len(remote0) {
  669. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  670. }
  671. g = globalList(m)
  672. if len(g) != len(remote0) {
  673. // the ones in remote0 remain
  674. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(remote0))
  675. }
  676. }
  677. func TestIssue4701(t *testing.T) {
  678. ldb := db.OpenMemory()
  679. s := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  680. localHave := fileList{
  681. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  682. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Invalid: true},
  683. }
  684. s.Update(protocol.LocalDeviceID, localHave)
  685. if c := s.LocalSize(); c.Files != 1 {
  686. t.Errorf("Expected 1 local file, got %v", c.Files)
  687. }
  688. if c := s.GlobalSize(); c.Files != 1 {
  689. t.Errorf("Expected 1 global file, got %v", c.Files)
  690. }
  691. localHave[1].Invalid = false
  692. s.Update(protocol.LocalDeviceID, localHave)
  693. if c := s.LocalSize(); c.Files != 2 {
  694. t.Errorf("Expected 2 local files, got %v", c.Files)
  695. }
  696. if c := s.GlobalSize(); c.Files != 2 {
  697. t.Errorf("Expected 2 global files, got %v", c.Files)
  698. }
  699. localHave[0].Invalid = true
  700. localHave[1].Invalid = true
  701. s.Update(protocol.LocalDeviceID, localHave)
  702. if c := s.LocalSize(); c.Files != 0 {
  703. t.Errorf("Expected 0 local files, got %v", c.Files)
  704. }
  705. if c := s.GlobalSize(); c.Files != 0 {
  706. t.Errorf("Expected 0 global files, got %v", c.Files)
  707. }
  708. }
  709. func replace(fs *db.FileSet, device protocol.DeviceID, files []protocol.FileInfo) {
  710. fs.Drop(device)
  711. fs.Update(device, files)
  712. }