set_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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, flags=%o\n", f.Name, f.Version, f.Size(), len(f.Blocks), f.Flags)
  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", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  86. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(2)},
  87. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(3)},
  88. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(4)},
  89. protocol.FileInfo{Name: "z", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(8)},
  90. }
  91. local1 := fileList{
  92. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  93. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(2)},
  94. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(3)},
  95. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(4)},
  96. protocol.FileInfo{Name: "z", Version: protocol.Vector{{ID: myID, Value: 1001}}, Flags: protocol.FlagDeleted},
  97. }
  98. localTot := fileList{
  99. local0[0],
  100. local0[1],
  101. local0[2],
  102. local0[3],
  103. protocol.FileInfo{Name: "z", Version: protocol.Vector{{ID: myID, Value: 1001}}, Flags: protocol.FlagDeleted},
  104. }
  105. remote0 := fileList{
  106. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  107. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(2)},
  108. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(5)},
  109. }
  110. remote1 := fileList{
  111. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(6)},
  112. protocol.FileInfo{Name: "e", Version: protocol.Vector{{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, globalDeleted, globalBytes := 0, 0, int64(0)
  146. for _, f := range g {
  147. if f.IsInvalid() {
  148. continue
  149. }
  150. if f.IsDeleted() {
  151. globalDeleted++
  152. } else {
  153. globalFiles++
  154. }
  155. globalBytes += f.Size()
  156. }
  157. gsFiles, gsDeleted, gsBytes := m.GlobalSize()
  158. if gsFiles != globalFiles {
  159. t.Errorf("Incorrect GlobalSize files; %d != %d", gsFiles, globalFiles)
  160. }
  161. if gsDeleted != globalDeleted {
  162. t.Errorf("Incorrect GlobalSize deleted; %d != %d", gsDeleted, globalDeleted)
  163. }
  164. if gsBytes != globalBytes {
  165. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gsBytes, globalBytes)
  166. }
  167. h := fileList(haveList(m, protocol.LocalDeviceID))
  168. sort.Sort(h)
  169. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  170. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot)
  171. }
  172. haveFiles, haveDeleted, haveBytes := 0, 0, int64(0)
  173. for _, f := range h {
  174. if f.IsInvalid() {
  175. continue
  176. }
  177. if f.IsDeleted() {
  178. haveDeleted++
  179. } else {
  180. haveFiles++
  181. }
  182. haveBytes += f.Size()
  183. }
  184. lsFiles, lsDeleted, lsBytes := m.LocalSize()
  185. if lsFiles != haveFiles {
  186. t.Errorf("Incorrect LocalSize files; %d != %d", lsFiles, haveFiles)
  187. }
  188. if lsDeleted != haveDeleted {
  189. t.Errorf("Incorrect LocalSize deleted; %d != %d", lsDeleted, haveDeleted)
  190. }
  191. if lsBytes != haveBytes {
  192. t.Errorf("Incorrect LocalSize bytes; %d != %d", lsBytes, haveBytes)
  193. }
  194. h = fileList(haveList(m, remoteDevice0))
  195. sort.Sort(h)
  196. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  197. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, remoteTot)
  198. }
  199. n := fileList(needList(m, protocol.LocalDeviceID))
  200. sort.Sort(n)
  201. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  202. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedLocalNeed)
  203. }
  204. n = fileList(needList(m, remoteDevice0))
  205. sort.Sort(n)
  206. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  207. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  208. }
  209. f, ok := m.Get(protocol.LocalDeviceID, "b")
  210. if !ok {
  211. t.Error("Unexpectedly not OK")
  212. }
  213. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  214. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  215. }
  216. f, ok = m.Get(remoteDevice0, "b")
  217. if !ok {
  218. t.Error("Unexpectedly not OK")
  219. }
  220. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  221. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  222. }
  223. f, ok = m.GetGlobal("b")
  224. if !ok {
  225. t.Error("Unexpectedly not OK")
  226. }
  227. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  228. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  229. }
  230. f, ok = m.Get(protocol.LocalDeviceID, "zz")
  231. if ok {
  232. t.Error("Unexpectedly OK")
  233. }
  234. if f.Name != "" {
  235. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  236. }
  237. f, ok = m.GetGlobal("zz")
  238. if ok {
  239. t.Error("Unexpectedly OK")
  240. }
  241. if f.Name != "" {
  242. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  243. }
  244. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  245. a := m.Availability("a")
  246. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  247. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  248. }
  249. a = m.Availability("b")
  250. if len(a) != 1 || a[0] != remoteDevice0 {
  251. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  252. }
  253. a = m.Availability("d")
  254. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  255. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  256. }
  257. }
  258. func TestNeedWithInvalid(t *testing.T) {
  259. ldb := db.OpenMemory()
  260. s := db.NewFileSet("test", ldb)
  261. localHave := fileList{
  262. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  263. }
  264. remote0Have := fileList{
  265. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  266. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  267. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  268. }
  269. remote1Have := fileList{
  270. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(7)},
  271. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  272. protocol.FileInfo{Name: "e", Version: protocol.Vector{{ID: myID, Value: 1004}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  273. }
  274. expectedNeed := fileList{
  275. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  276. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(7)},
  277. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  278. }
  279. s.Replace(protocol.LocalDeviceID, localHave)
  280. s.Replace(remoteDevice0, remote0Have)
  281. s.Replace(remoteDevice1, remote1Have)
  282. need := fileList(needList(s, protocol.LocalDeviceID))
  283. sort.Sort(need)
  284. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  285. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  286. }
  287. }
  288. func TestUpdateToInvalid(t *testing.T) {
  289. ldb := db.OpenMemory()
  290. s := db.NewFileSet("test", ldb)
  291. localHave := fileList{
  292. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  293. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  294. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  295. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  296. }
  297. s.Replace(protocol.LocalDeviceID, localHave)
  298. have := fileList(haveList(s, protocol.LocalDeviceID))
  299. sort.Sort(have)
  300. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  301. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  302. }
  303. localHave[1] = protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Flags: protocol.FlagInvalid}
  304. s.Update(protocol.LocalDeviceID, localHave[1:2])
  305. have = fileList(haveList(s, protocol.LocalDeviceID))
  306. sort.Sort(have)
  307. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  308. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  309. }
  310. }
  311. func TestInvalidAvailability(t *testing.T) {
  312. ldb := db.OpenMemory()
  313. s := db.NewFileSet("test", ldb)
  314. remote0Have := fileList{
  315. protocol.FileInfo{Name: "both", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  316. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  317. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  318. protocol.FileInfo{Name: "none", Version: protocol.Vector{{ID: myID, Value: 1004}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  319. }
  320. remote1Have := fileList{
  321. protocol.FileInfo{Name: "both", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  322. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(7)},
  323. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  324. protocol.FileInfo{Name: "none", Version: protocol.Vector{{ID: myID, Value: 1004}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  325. }
  326. s.Replace(remoteDevice0, remote0Have)
  327. s.Replace(remoteDevice1, remote1Have)
  328. if av := s.Availability("both"); len(av) != 2 {
  329. t.Error("Incorrect availability for 'both':", av)
  330. }
  331. if av := s.Availability("r0only"); len(av) != 1 || av[0] != remoteDevice0 {
  332. t.Error("Incorrect availability for 'r0only':", av)
  333. }
  334. if av := s.Availability("r1only"); len(av) != 1 || av[0] != remoteDevice1 {
  335. t.Error("Incorrect availability for 'r1only':", av)
  336. }
  337. if av := s.Availability("none"); len(av) != 0 {
  338. t.Error("Incorrect availability for 'none':", av)
  339. }
  340. }
  341. func TestGlobalReset(t *testing.T) {
  342. ldb := db.OpenMemory()
  343. m := db.NewFileSet("test", ldb)
  344. local := []protocol.FileInfo{
  345. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  346. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  347. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  348. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  349. }
  350. remote := []protocol.FileInfo{
  351. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  352. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}},
  353. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  354. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  355. }
  356. m.Replace(protocol.LocalDeviceID, local)
  357. g := globalList(m)
  358. sort.Sort(fileList(g))
  359. if fmt.Sprint(g) != fmt.Sprint(local) {
  360. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  361. }
  362. m.Replace(remoteDevice0, remote)
  363. m.Replace(remoteDevice0, nil)
  364. g = globalList(m)
  365. sort.Sort(fileList(g))
  366. if fmt.Sprint(g) != fmt.Sprint(local) {
  367. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  368. }
  369. }
  370. func TestNeed(t *testing.T) {
  371. ldb := db.OpenMemory()
  372. m := db.NewFileSet("test", ldb)
  373. local := []protocol.FileInfo{
  374. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  375. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  376. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  377. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  378. }
  379. remote := []protocol.FileInfo{
  380. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  381. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}},
  382. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  383. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  384. }
  385. shouldNeed := []protocol.FileInfo{
  386. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}},
  387. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  388. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  389. }
  390. m.Replace(protocol.LocalDeviceID, local)
  391. m.Replace(remoteDevice0, remote)
  392. need := needList(m, protocol.LocalDeviceID)
  393. sort.Sort(fileList(need))
  394. sort.Sort(fileList(shouldNeed))
  395. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  396. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  397. }
  398. }
  399. func TestLocalVersion(t *testing.T) {
  400. ldb := db.OpenMemory()
  401. m := db.NewFileSet("test", ldb)
  402. local1 := []protocol.FileInfo{
  403. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  404. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  405. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  406. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  407. }
  408. local2 := []protocol.FileInfo{
  409. local1[0],
  410. // [1] deleted
  411. local1[2],
  412. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  413. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  414. }
  415. m.Replace(protocol.LocalDeviceID, local1)
  416. c0 := m.LocalVersion(protocol.LocalDeviceID)
  417. m.Replace(protocol.LocalDeviceID, local2)
  418. c1 := m.LocalVersion(protocol.LocalDeviceID)
  419. if !(c1 > c0) {
  420. t.Fatal("Local version number should have incremented")
  421. }
  422. }
  423. func TestListDropFolder(t *testing.T) {
  424. ldb := db.OpenMemory()
  425. s0 := db.NewFileSet("test0", ldb)
  426. local1 := []protocol.FileInfo{
  427. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  428. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  429. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  430. }
  431. s0.Replace(protocol.LocalDeviceID, local1)
  432. s1 := db.NewFileSet("test1", ldb)
  433. local2 := []protocol.FileInfo{
  434. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  435. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  436. {Name: "f", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  437. }
  438. s1.Replace(remoteDevice0, local2)
  439. // Check that we have both folders and their data is in the global list
  440. expectedFolderList := []string{"test0", "test1"}
  441. actualFolderList := ldb.ListFolders()
  442. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  443. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  444. }
  445. if l := len(globalList(s0)); l != 3 {
  446. t.Errorf("Incorrect global length %d != 3 for s0", l)
  447. }
  448. if l := len(globalList(s1)); l != 3 {
  449. t.Errorf("Incorrect global length %d != 3 for s1", l)
  450. }
  451. // Drop one of them and check that it's gone.
  452. db.DropFolder(ldb, "test1")
  453. expectedFolderList = []string{"test0"}
  454. actualFolderList = ldb.ListFolders()
  455. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  456. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  457. }
  458. if l := len(globalList(s0)); l != 3 {
  459. t.Errorf("Incorrect global length %d != 3 for s0", l)
  460. }
  461. if l := len(globalList(s1)); l != 0 {
  462. t.Errorf("Incorrect global length %d != 0 for s1", l)
  463. }
  464. }
  465. func TestGlobalNeedWithInvalid(t *testing.T) {
  466. ldb := db.OpenMemory()
  467. s := db.NewFileSet("test1", ldb)
  468. rem0 := fileList{
  469. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  470. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1002}}, Flags: protocol.FlagInvalid},
  471. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  472. }
  473. s.Replace(remoteDevice0, rem0)
  474. rem1 := fileList{
  475. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  476. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  477. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Flags: protocol.FlagInvalid},
  478. }
  479. s.Replace(remoteDevice1, rem1)
  480. total := fileList{
  481. // There's a valid copy of each file, so it should be merged
  482. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  483. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  484. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  485. }
  486. need := fileList(needList(s, protocol.LocalDeviceID))
  487. if fmt.Sprint(need) != fmt.Sprint(total) {
  488. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
  489. }
  490. global := fileList(globalList(s))
  491. if fmt.Sprint(global) != fmt.Sprint(total) {
  492. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
  493. }
  494. }
  495. func TestLongPath(t *testing.T) {
  496. ldb := db.OpenMemory()
  497. s := db.NewFileSet("test", ldb)
  498. var b bytes.Buffer
  499. for i := 0; i < 100; i++ {
  500. b.WriteString("012345678901234567890123456789012345678901234567890")
  501. }
  502. name := b.String() // 5000 characters
  503. local := []protocol.FileInfo{
  504. {Name: string(name), Version: protocol.Vector{{ID: myID, Value: 1000}}},
  505. }
  506. s.Replace(protocol.LocalDeviceID, local)
  507. gf := globalList(s)
  508. if l := len(gf); l != 1 {
  509. t.Fatalf("Incorrect len %d != 1 for global list", l)
  510. }
  511. if gf[0].Name != local[0].Name {
  512. t.Errorf("Incorrect long filename;\n%q !=\n%q",
  513. gf[0].Name, local[0].Name)
  514. }
  515. }
  516. func TestCommitted(t *testing.T) {
  517. // Verify that the Committed counter increases when we change things and
  518. // doesn't increase when we don't.
  519. ldb := db.OpenMemory()
  520. s := db.NewFileSet("test", ldb)
  521. local := []protocol.FileInfo{
  522. {Name: string("file"), Version: protocol.Vector{{ID: myID, Value: 1000}}},
  523. }
  524. // Adding a file should increase the counter
  525. c0 := ldb.Committed()
  526. s.Replace(protocol.LocalDeviceID, local)
  527. c1 := ldb.Committed()
  528. if c1 <= c0 {
  529. t.Errorf("committed data didn't increase; %d <= %d", c1, c0)
  530. }
  531. // Updating with something identical should not do anything
  532. s.Update(protocol.LocalDeviceID, local)
  533. c2 := ldb.Committed()
  534. if c2 > c1 {
  535. t.Errorf("replace with same contents should do nothing but %d > %d", c2, c1)
  536. }
  537. }
  538. func BenchmarkUpdateOneFile(b *testing.B) {
  539. local0 := fileList{
  540. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  541. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(2)},
  542. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(3)},
  543. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(4)},
  544. // A longer name is more realistic and causes more allocations
  545. protocol.FileInfo{Name: "zajksdhaskjdh/askjdhaskjdashkajshd/kasjdhaskjdhaskdjhaskdjash/dkjashdaksjdhaskdjahskdjh", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(8)},
  546. }
  547. ldb, err := db.Open("testdata/benchmarkupdate.db")
  548. if err != nil {
  549. b.Fatal(err)
  550. }
  551. defer func() {
  552. ldb.Close()
  553. os.RemoveAll("testdata/benchmarkupdate.db")
  554. }()
  555. m := db.NewFileSet("test", ldb)
  556. m.Replace(protocol.LocalDeviceID, local0)
  557. l := local0[4:5]
  558. for i := 0; i < b.N; i++ {
  559. l[0].Version = l[0].Version.Update(myID)
  560. m.Update(protocol.LocalDeviceID, local0)
  561. }
  562. b.ReportAllocs()
  563. }