set_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. "reflect"
  11. "sort"
  12. "testing"
  13. "github.com/syncthing/syncthing/lib/db"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. )
  16. var remoteDevice0, remoteDevice1 protocol.DeviceID
  17. func init() {
  18. remoteDevice0, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  19. remoteDevice1, _ = protocol.DeviceIDFromString("I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU")
  20. }
  21. const myID = 1
  22. func genBlocks(n int) []protocol.BlockInfo {
  23. b := make([]protocol.BlockInfo, n)
  24. for i := range b {
  25. h := make([]byte, 32)
  26. for j := range h {
  27. h[j] = byte(i + j)
  28. }
  29. b[i].Size = int32(i)
  30. b[i].Hash = h
  31. }
  32. return b
  33. }
  34. func globalList(s *db.FileSet) []protocol.FileInfo {
  35. var fs []protocol.FileInfo
  36. s.WithGlobal(func(fi db.FileIntf) bool {
  37. f := fi.(protocol.FileInfo)
  38. fs = append(fs, f)
  39. return true
  40. })
  41. return fs
  42. }
  43. func haveList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  44. var fs []protocol.FileInfo
  45. s.WithHave(n, func(fi db.FileIntf) bool {
  46. f := fi.(protocol.FileInfo)
  47. fs = append(fs, f)
  48. return true
  49. })
  50. return fs
  51. }
  52. func needList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  53. var fs []protocol.FileInfo
  54. s.WithNeed(n, func(fi db.FileIntf) bool {
  55. f := fi.(protocol.FileInfo)
  56. fs = append(fs, f)
  57. return true
  58. })
  59. return fs
  60. }
  61. type fileList []protocol.FileInfo
  62. func (l fileList) Len() int {
  63. return len(l)
  64. }
  65. func (l fileList) Less(a, b int) bool {
  66. return l[a].Name < l[b].Name
  67. }
  68. func (l fileList) Swap(a, b int) {
  69. l[a], l[b] = l[b], l[a]
  70. }
  71. func (l fileList) String() string {
  72. var b bytes.Buffer
  73. b.WriteString("[]protocol.FileList{\n")
  74. for _, f := range l {
  75. fmt.Fprintf(&b, " %q: #%d, %d bytes, %d blocks, flags=%o\n", f.Name, f.Version, f.Size(), len(f.Blocks), f.Flags)
  76. }
  77. b.WriteString("}")
  78. return b.String()
  79. }
  80. func TestGlobalSet(t *testing.T) {
  81. ldb := db.OpenMemory()
  82. m := db.NewFileSet("test", ldb)
  83. local0 := fileList{
  84. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  85. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(2)},
  86. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(3)},
  87. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(4)},
  88. protocol.FileInfo{Name: "z", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(8)},
  89. }
  90. local1 := fileList{
  91. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  92. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(2)},
  93. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(3)},
  94. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(4)},
  95. protocol.FileInfo{Name: "z", Version: protocol.Vector{{ID: myID, Value: 1001}}, Flags: protocol.FlagDeleted},
  96. }
  97. localTot := fileList{
  98. local0[0],
  99. local0[1],
  100. local0[2],
  101. local0[3],
  102. protocol.FileInfo{Name: "z", Version: protocol.Vector{{ID: myID, Value: 1001}}, Flags: protocol.FlagDeleted},
  103. }
  104. remote0 := fileList{
  105. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  106. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(2)},
  107. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(5)},
  108. }
  109. remote1 := fileList{
  110. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(6)},
  111. protocol.FileInfo{Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(7)},
  112. }
  113. remoteTot := fileList{
  114. remote0[0],
  115. remote1[0],
  116. remote0[2],
  117. remote1[1],
  118. }
  119. expectedGlobal := fileList{
  120. remote0[0], // a
  121. remote1[0], // b
  122. remote0[2], // c
  123. localTot[3], // d
  124. remote1[1], // e
  125. localTot[4], // z
  126. }
  127. expectedLocalNeed := fileList{
  128. remote1[0],
  129. remote0[2],
  130. remote1[1],
  131. }
  132. expectedRemoteNeed := fileList{
  133. local0[3],
  134. }
  135. m.Replace(protocol.LocalDeviceID, local0)
  136. m.Replace(protocol.LocalDeviceID, local1)
  137. m.Replace(remoteDevice0, remote0)
  138. m.Update(remoteDevice0, remote1)
  139. g := fileList(globalList(m))
  140. sort.Sort(g)
  141. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
  142. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal)
  143. }
  144. globalFiles, globalDeleted, globalBytes := 0, 0, int64(0)
  145. for _, f := range g {
  146. if f.IsInvalid() {
  147. continue
  148. }
  149. if f.IsDeleted() {
  150. globalDeleted++
  151. } else {
  152. globalFiles++
  153. }
  154. globalBytes += f.Size()
  155. }
  156. gsFiles, gsDeleted, gsBytes := m.GlobalSize()
  157. if gsFiles != globalFiles {
  158. t.Errorf("Incorrect GlobalSize files; %d != %d", gsFiles, globalFiles)
  159. }
  160. if gsDeleted != globalDeleted {
  161. t.Errorf("Incorrect GlobalSize deleted; %d != %d", gsDeleted, globalDeleted)
  162. }
  163. if gsBytes != globalBytes {
  164. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gsBytes, globalBytes)
  165. }
  166. h := fileList(haveList(m, protocol.LocalDeviceID))
  167. sort.Sort(h)
  168. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  169. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot)
  170. }
  171. haveFiles, haveDeleted, haveBytes := 0, 0, int64(0)
  172. for _, f := range h {
  173. if f.IsInvalid() {
  174. continue
  175. }
  176. if f.IsDeleted() {
  177. haveDeleted++
  178. } else {
  179. haveFiles++
  180. }
  181. haveBytes += f.Size()
  182. }
  183. lsFiles, lsDeleted, lsBytes := m.LocalSize()
  184. if lsFiles != haveFiles {
  185. t.Errorf("Incorrect LocalSize files; %d != %d", lsFiles, haveFiles)
  186. }
  187. if lsDeleted != haveDeleted {
  188. t.Errorf("Incorrect LocalSize deleted; %d != %d", lsDeleted, haveDeleted)
  189. }
  190. if lsBytes != haveBytes {
  191. t.Errorf("Incorrect LocalSize bytes; %d != %d", lsBytes, haveBytes)
  192. }
  193. h = fileList(haveList(m, remoteDevice0))
  194. sort.Sort(h)
  195. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  196. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, remoteTot)
  197. }
  198. n := fileList(needList(m, protocol.LocalDeviceID))
  199. sort.Sort(n)
  200. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  201. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedLocalNeed)
  202. }
  203. n = fileList(needList(m, remoteDevice0))
  204. sort.Sort(n)
  205. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  206. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  207. }
  208. f, ok := m.Get(protocol.LocalDeviceID, "b")
  209. if !ok {
  210. t.Error("Unexpectedly not OK")
  211. }
  212. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  213. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  214. }
  215. f, ok = m.Get(remoteDevice0, "b")
  216. if !ok {
  217. t.Error("Unexpectedly not OK")
  218. }
  219. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  220. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  221. }
  222. f, ok = m.GetGlobal("b")
  223. if !ok {
  224. t.Error("Unexpectedly not OK")
  225. }
  226. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  227. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  228. }
  229. f, ok = m.Get(protocol.LocalDeviceID, "zz")
  230. if ok {
  231. t.Error("Unexpectedly OK")
  232. }
  233. if f.Name != "" {
  234. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  235. }
  236. f, ok = m.GetGlobal("zz")
  237. if ok {
  238. t.Error("Unexpectedly OK")
  239. }
  240. if f.Name != "" {
  241. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  242. }
  243. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  244. a := m.Availability("a")
  245. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  246. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  247. }
  248. a = m.Availability("b")
  249. if len(a) != 1 || a[0] != remoteDevice0 {
  250. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  251. }
  252. a = m.Availability("d")
  253. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  254. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  255. }
  256. }
  257. func TestNeedWithInvalid(t *testing.T) {
  258. ldb := db.OpenMemory()
  259. s := db.NewFileSet("test", ldb)
  260. localHave := fileList{
  261. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  262. }
  263. remote0Have := fileList{
  264. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  265. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  266. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  267. }
  268. remote1Have := fileList{
  269. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(7)},
  270. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  271. protocol.FileInfo{Name: "e", Version: protocol.Vector{{ID: myID, Value: 1004}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  272. }
  273. expectedNeed := fileList{
  274. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  275. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(7)},
  276. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  277. }
  278. s.Replace(protocol.LocalDeviceID, localHave)
  279. s.Replace(remoteDevice0, remote0Have)
  280. s.Replace(remoteDevice1, remote1Have)
  281. need := fileList(needList(s, protocol.LocalDeviceID))
  282. sort.Sort(need)
  283. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  284. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  285. }
  286. }
  287. func TestUpdateToInvalid(t *testing.T) {
  288. ldb := db.OpenMemory()
  289. s := db.NewFileSet("test", ldb)
  290. localHave := fileList{
  291. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  292. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  293. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  294. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  295. }
  296. s.Replace(protocol.LocalDeviceID, localHave)
  297. have := fileList(haveList(s, protocol.LocalDeviceID))
  298. sort.Sort(have)
  299. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  300. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  301. }
  302. localHave[1] = protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Flags: protocol.FlagInvalid}
  303. s.Update(protocol.LocalDeviceID, localHave[1:2])
  304. have = fileList(haveList(s, protocol.LocalDeviceID))
  305. sort.Sort(have)
  306. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  307. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  308. }
  309. }
  310. func TestInvalidAvailability(t *testing.T) {
  311. ldb := db.OpenMemory()
  312. s := db.NewFileSet("test", ldb)
  313. remote0Have := fileList{
  314. protocol.FileInfo{Name: "both", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  315. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  316. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  317. protocol.FileInfo{Name: "none", Version: protocol.Vector{{ID: myID, Value: 1004}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  318. }
  319. remote1Have := fileList{
  320. protocol.FileInfo{Name: "both", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  321. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(7)},
  322. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  323. protocol.FileInfo{Name: "none", Version: protocol.Vector{{ID: myID, Value: 1004}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  324. }
  325. s.Replace(remoteDevice0, remote0Have)
  326. s.Replace(remoteDevice1, remote1Have)
  327. if av := s.Availability("both"); len(av) != 2 {
  328. t.Error("Incorrect availability for 'both':", av)
  329. }
  330. if av := s.Availability("r0only"); len(av) != 1 || av[0] != remoteDevice0 {
  331. t.Error("Incorrect availability for 'r0only':", av)
  332. }
  333. if av := s.Availability("r1only"); len(av) != 1 || av[0] != remoteDevice1 {
  334. t.Error("Incorrect availability for 'r1only':", av)
  335. }
  336. if av := s.Availability("none"); len(av) != 0 {
  337. t.Error("Incorrect availability for 'none':", av)
  338. }
  339. }
  340. func TestGlobalReset(t *testing.T) {
  341. ldb := db.OpenMemory()
  342. m := db.NewFileSet("test", ldb)
  343. local := []protocol.FileInfo{
  344. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  345. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  346. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  347. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  348. }
  349. remote := []protocol.FileInfo{
  350. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  351. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}},
  352. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  353. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  354. }
  355. m.Replace(protocol.LocalDeviceID, local)
  356. g := globalList(m)
  357. sort.Sort(fileList(g))
  358. if fmt.Sprint(g) != fmt.Sprint(local) {
  359. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  360. }
  361. m.Replace(remoteDevice0, remote)
  362. m.Replace(remoteDevice0, nil)
  363. g = globalList(m)
  364. sort.Sort(fileList(g))
  365. if fmt.Sprint(g) != fmt.Sprint(local) {
  366. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  367. }
  368. }
  369. func TestNeed(t *testing.T) {
  370. ldb := db.OpenMemory()
  371. m := db.NewFileSet("test", ldb)
  372. local := []protocol.FileInfo{
  373. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  374. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  375. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  376. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  377. }
  378. remote := []protocol.FileInfo{
  379. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  380. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}},
  381. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  382. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  383. }
  384. shouldNeed := []protocol.FileInfo{
  385. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}},
  386. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  387. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  388. }
  389. m.Replace(protocol.LocalDeviceID, local)
  390. m.Replace(remoteDevice0, remote)
  391. need := needList(m, protocol.LocalDeviceID)
  392. sort.Sort(fileList(need))
  393. sort.Sort(fileList(shouldNeed))
  394. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  395. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  396. }
  397. }
  398. func TestLocalVersion(t *testing.T) {
  399. ldb := db.OpenMemory()
  400. m := db.NewFileSet("test", ldb)
  401. local1 := []protocol.FileInfo{
  402. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  403. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  404. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  405. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  406. }
  407. local2 := []protocol.FileInfo{
  408. local1[0],
  409. // [1] deleted
  410. local1[2],
  411. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  412. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  413. }
  414. m.Replace(protocol.LocalDeviceID, local1)
  415. c0 := m.LocalVersion(protocol.LocalDeviceID)
  416. m.Replace(protocol.LocalDeviceID, local2)
  417. c1 := m.LocalVersion(protocol.LocalDeviceID)
  418. if !(c1 > c0) {
  419. t.Fatal("Local version number should have incremented")
  420. }
  421. }
  422. func TestListDropFolder(t *testing.T) {
  423. ldb := db.OpenMemory()
  424. s0 := db.NewFileSet("test0", ldb)
  425. local1 := []protocol.FileInfo{
  426. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  427. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  428. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  429. }
  430. s0.Replace(protocol.LocalDeviceID, local1)
  431. s1 := db.NewFileSet("test1", ldb)
  432. local2 := []protocol.FileInfo{
  433. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  434. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  435. {Name: "f", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  436. }
  437. s1.Replace(remoteDevice0, local2)
  438. // Check that we have both folders and their data is in the global list
  439. expectedFolderList := []string{"test0", "test1"}
  440. if actualFolderList := ldb.ListFolders(); !reflect.DeepEqual(actualFolderList, expectedFolderList) {
  441. t.Fatalf("FolderList mismatch\nE: %v\nA: %v", expectedFolderList, actualFolderList)
  442. }
  443. if l := len(globalList(s0)); l != 3 {
  444. t.Errorf("Incorrect global length %d != 3 for s0", l)
  445. }
  446. if l := len(globalList(s1)); l != 3 {
  447. t.Errorf("Incorrect global length %d != 3 for s1", l)
  448. }
  449. // Drop one of them and check that it's gone.
  450. db.DropFolder(ldb, "test1")
  451. expectedFolderList = []string{"test0"}
  452. if actualFolderList := ldb.ListFolders(); !reflect.DeepEqual(actualFolderList, expectedFolderList) {
  453. t.Fatalf("FolderList mismatch\nE: %v\nA: %v", expectedFolderList, actualFolderList)
  454. }
  455. if l := len(globalList(s0)); l != 3 {
  456. t.Errorf("Incorrect global length %d != 3 for s0", l)
  457. }
  458. if l := len(globalList(s1)); l != 0 {
  459. t.Errorf("Incorrect global length %d != 0 for s1", l)
  460. }
  461. }
  462. func TestGlobalNeedWithInvalid(t *testing.T) {
  463. ldb := db.OpenMemory()
  464. s := db.NewFileSet("test1", ldb)
  465. rem0 := fileList{
  466. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  467. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1002}}, Flags: protocol.FlagInvalid},
  468. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  469. }
  470. s.Replace(remoteDevice0, rem0)
  471. rem1 := fileList{
  472. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  473. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  474. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Flags: protocol.FlagInvalid},
  475. }
  476. s.Replace(remoteDevice1, rem1)
  477. total := fileList{
  478. // There's a valid copy of each file, so it should be merged
  479. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  480. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  481. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  482. }
  483. need := fileList(needList(s, protocol.LocalDeviceID))
  484. if fmt.Sprint(need) != fmt.Sprint(total) {
  485. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
  486. }
  487. global := fileList(globalList(s))
  488. if fmt.Sprint(global) != fmt.Sprint(total) {
  489. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
  490. }
  491. }
  492. func TestLongPath(t *testing.T) {
  493. ldb := db.OpenMemory()
  494. s := db.NewFileSet("test", ldb)
  495. var b bytes.Buffer
  496. for i := 0; i < 100; i++ {
  497. b.WriteString("012345678901234567890123456789012345678901234567890")
  498. }
  499. name := b.String() // 5000 characters
  500. local := []protocol.FileInfo{
  501. {Name: string(name), Version: protocol.Vector{{ID: myID, Value: 1000}}},
  502. }
  503. s.Replace(protocol.LocalDeviceID, local)
  504. gf := globalList(s)
  505. if l := len(gf); l != 1 {
  506. t.Fatalf("Incorrect len %d != 1 for global list", l)
  507. }
  508. if gf[0].Name != local[0].Name {
  509. t.Errorf("Incorrect long filename;\n%q !=\n%q",
  510. gf[0].Name, local[0].Name)
  511. }
  512. }