set_test.go 25 KB

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