set_test.go 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  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. "path/filepath"
  12. "sort"
  13. "testing"
  14. "time"
  15. "github.com/d4l3k/messagediff"
  16. "github.com/syncthing/syncthing/lib/db"
  17. "github.com/syncthing/syncthing/lib/db/backend"
  18. "github.com/syncthing/syncthing/lib/events"
  19. "github.com/syncthing/syncthing/lib/protocol"
  20. )
  21. var remoteDevice0, remoteDevice1 protocol.DeviceID
  22. func init() {
  23. remoteDevice0, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  24. remoteDevice1, _ = protocol.DeviceIDFromString("I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU")
  25. }
  26. const myID = 1
  27. func genBlocks(n int) []protocol.BlockInfo {
  28. b := make([]protocol.BlockInfo, n)
  29. for i := range b {
  30. h := make([]byte, 32)
  31. for j := range h {
  32. h[j] = byte(i + j)
  33. }
  34. b[i].Size = i
  35. b[i].Hash = h
  36. }
  37. return b
  38. }
  39. func globalList(t testing.TB, s *db.FileSet) []protocol.FileInfo {
  40. var fs []protocol.FileInfo
  41. snap := snapshot(t, s)
  42. defer snap.Release()
  43. snap.WithGlobal(func(fi protocol.FileIntf) bool {
  44. f := fi.(protocol.FileInfo)
  45. fs = append(fs, f)
  46. return true
  47. })
  48. return fs
  49. }
  50. func globalListPrefixed(t testing.TB, s *db.FileSet, prefix string) []db.FileInfoTruncated {
  51. var fs []db.FileInfoTruncated
  52. snap := snapshot(t, s)
  53. defer snap.Release()
  54. snap.WithPrefixedGlobalTruncated(prefix, func(fi protocol.FileIntf) bool {
  55. f := fi.(db.FileInfoTruncated)
  56. fs = append(fs, f)
  57. return true
  58. })
  59. return fs
  60. }
  61. func haveList(t testing.TB, s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  62. var fs []protocol.FileInfo
  63. snap := snapshot(t, s)
  64. defer snap.Release()
  65. snap.WithHave(n, func(fi protocol.FileIntf) bool {
  66. f := fi.(protocol.FileInfo)
  67. fs = append(fs, f)
  68. return true
  69. })
  70. return fs
  71. }
  72. func haveListPrefixed(t testing.TB, s *db.FileSet, n protocol.DeviceID, prefix string) []db.FileInfoTruncated {
  73. var fs []db.FileInfoTruncated
  74. snap := snapshot(t, s)
  75. defer snap.Release()
  76. snap.WithPrefixedHaveTruncated(n, prefix, func(fi protocol.FileIntf) bool {
  77. f := fi.(db.FileInfoTruncated)
  78. fs = append(fs, f)
  79. return true
  80. })
  81. return fs
  82. }
  83. func needList(t testing.TB, s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  84. var fs []protocol.FileInfo
  85. snap := snapshot(t, s)
  86. defer snap.Release()
  87. snap.WithNeed(n, func(fi protocol.FileIntf) bool {
  88. f := fi.(protocol.FileInfo)
  89. fs = append(fs, f)
  90. return true
  91. })
  92. return fs
  93. }
  94. type fileList []protocol.FileInfo
  95. func (l fileList) Len() int {
  96. return len(l)
  97. }
  98. func (l fileList) Less(a, b int) bool {
  99. return l[a].Name < l[b].Name
  100. }
  101. func (l fileList) Swap(a, b int) {
  102. l[a], l[b] = l[b], l[a]
  103. }
  104. func (l fileList) String() string {
  105. var b bytes.Buffer
  106. b.WriteString("[]protocol.FileList{\n")
  107. for _, f := range l {
  108. fmt.Fprintf(&b, " %q: #%v, %d bytes, %d blocks, perms=%o\n", f.Name, f.Version, f.Size, len(f.Blocks), f.Permissions)
  109. }
  110. b.WriteString("}")
  111. return b.String()
  112. }
  113. func setSequence(seq int64, files fileList) int64 {
  114. for i := range files {
  115. seq++
  116. files[i].Sequence = seq
  117. }
  118. return seq
  119. }
  120. func setBlocksHash(files fileList) {
  121. for i, f := range files {
  122. files[i].BlocksHash = protocol.BlocksHash(f.Blocks)
  123. }
  124. }
  125. func TestGlobalSet(t *testing.T) {
  126. ldb := newLowlevelMemory(t)
  127. defer ldb.Close()
  128. m := newFileSet(t, "test", ldb)
  129. local0 := fileList{
  130. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  131. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  132. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  133. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  134. protocol.FileInfo{Name: "z", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  135. }
  136. localSeq := setSequence(0, local0)
  137. setBlocksHash(local0)
  138. local1 := fileList{
  139. protocol.FileInfo{Name: "a", Sequence: 6, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  140. protocol.FileInfo{Name: "b", Sequence: 7, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  141. protocol.FileInfo{Name: "c", Sequence: 8, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  142. protocol.FileInfo{Name: "d", Sequence: 9, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  143. protocol.FileInfo{Name: "z", Sequence: 10, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  144. }
  145. setSequence(localSeq, local1)
  146. setBlocksHash(local1)
  147. localTot := fileList{
  148. local1[0],
  149. local1[1],
  150. local1[2],
  151. local1[3],
  152. protocol.FileInfo{Name: "z", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  153. }
  154. remote0 := fileList{
  155. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  156. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  157. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  158. }
  159. remoteSeq := setSequence(0, remote0)
  160. setBlocksHash(remote0)
  161. remote1 := fileList{
  162. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(6)},
  163. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(7)},
  164. }
  165. setSequence(remoteSeq, remote1)
  166. setBlocksHash(remote1)
  167. remoteTot := fileList{
  168. remote0[0],
  169. remote1[0],
  170. remote0[2],
  171. remote1[1],
  172. }
  173. expectedGlobal := fileList{
  174. remote0[0], // a
  175. remote1[0], // b
  176. remote0[2], // c
  177. localTot[3], // d
  178. remote1[1], // e
  179. localTot[4], // z
  180. }
  181. expectedLocalNeed := fileList{
  182. remote1[0],
  183. remote0[2],
  184. remote1[1],
  185. }
  186. expectedRemoteNeed := fileList{
  187. local0[3],
  188. }
  189. replace(m, protocol.LocalDeviceID, local0)
  190. replace(m, protocol.LocalDeviceID, local1)
  191. replace(m, remoteDevice0, remote0)
  192. m.Update(remoteDevice0, remote1)
  193. check := func() {
  194. t.Helper()
  195. g := fileList(globalList(t, m))
  196. sort.Sort(g)
  197. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
  198. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal)
  199. }
  200. var globalFiles, globalDirectories, globalDeleted int
  201. var globalBytes int64
  202. for _, f := range g {
  203. if f.IsInvalid() {
  204. continue
  205. }
  206. switch {
  207. case f.IsDeleted():
  208. globalDeleted++
  209. case f.IsDirectory():
  210. globalDirectories++
  211. default:
  212. globalFiles++
  213. }
  214. globalBytes += f.FileSize()
  215. }
  216. gs := globalSize(t, m)
  217. if gs.Files != globalFiles {
  218. t.Errorf("Incorrect GlobalSize files; %d != %d", gs.Files, globalFiles)
  219. }
  220. if gs.Directories != globalDirectories {
  221. t.Errorf("Incorrect GlobalSize directories; %d != %d", gs.Directories, globalDirectories)
  222. }
  223. if gs.Deleted != globalDeleted {
  224. t.Errorf("Incorrect GlobalSize deleted; %d != %d", gs.Deleted, globalDeleted)
  225. }
  226. if gs.Bytes != globalBytes {
  227. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  228. }
  229. h := fileList(haveList(t, m, protocol.LocalDeviceID))
  230. sort.Sort(h)
  231. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  232. t.Errorf("Have incorrect (local);\n A: %v !=\n E: %v", h, localTot)
  233. }
  234. var haveFiles, haveDirectories, haveDeleted int
  235. var haveBytes int64
  236. for _, f := range h {
  237. if f.IsInvalid() {
  238. continue
  239. }
  240. switch {
  241. case f.IsDeleted():
  242. haveDeleted++
  243. case f.IsDirectory():
  244. haveDirectories++
  245. default:
  246. haveFiles++
  247. }
  248. haveBytes += f.FileSize()
  249. }
  250. ls := localSize(t, m)
  251. if ls.Files != haveFiles {
  252. t.Errorf("Incorrect LocalSize files; %d != %d", ls.Files, haveFiles)
  253. }
  254. if ls.Directories != haveDirectories {
  255. t.Errorf("Incorrect LocalSize directories; %d != %d", ls.Directories, haveDirectories)
  256. }
  257. if ls.Deleted != haveDeleted {
  258. t.Errorf("Incorrect LocalSize deleted; %d != %d", ls.Deleted, haveDeleted)
  259. }
  260. if ls.Bytes != haveBytes {
  261. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  262. }
  263. h = fileList(haveList(t, m, remoteDevice0))
  264. sort.Sort(h)
  265. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  266. t.Errorf("Have incorrect (remote);\n A: %v !=\n E: %v", h, remoteTot)
  267. }
  268. n := fileList(needList(t, m, protocol.LocalDeviceID))
  269. sort.Sort(n)
  270. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  271. t.Errorf("Need incorrect (local);\n A: %v !=\n E: %v", n, expectedLocalNeed)
  272. }
  273. checkNeed(t, m, protocol.LocalDeviceID, expectedLocalNeed)
  274. n = fileList(needList(t, m, remoteDevice0))
  275. sort.Sort(n)
  276. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  277. t.Errorf("Need incorrect (remote);\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  278. }
  279. checkNeed(t, m, remoteDevice0, expectedRemoteNeed)
  280. snap := snapshot(t, m)
  281. defer snap.Release()
  282. f, ok := snap.Get(protocol.LocalDeviceID, "b")
  283. if !ok {
  284. t.Error("Unexpectedly not OK")
  285. }
  286. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  287. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  288. }
  289. f, ok = snap.Get(remoteDevice0, "b")
  290. if !ok {
  291. t.Error("Unexpectedly not OK")
  292. }
  293. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  294. t.Errorf("Get incorrect (remote);\n A: %v !=\n E: %v", f, remote1[0])
  295. }
  296. f, ok = snap.GetGlobal("b")
  297. if !ok {
  298. t.Error("Unexpectedly not OK")
  299. }
  300. if fmt.Sprint(f) != fmt.Sprint(expectedGlobal[1]) {
  301. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  302. }
  303. f, ok = snap.Get(protocol.LocalDeviceID, "zz")
  304. if ok {
  305. t.Error("Unexpectedly OK")
  306. }
  307. if f.Name != "" {
  308. t.Errorf("Get incorrect (local);\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  309. }
  310. f, ok = snap.GetGlobal("zz")
  311. if ok {
  312. t.Error("Unexpectedly OK")
  313. }
  314. if f.Name != "" {
  315. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  316. }
  317. }
  318. check()
  319. snap := snapshot(t, m)
  320. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  321. a := snap.Availability("a")
  322. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  323. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  324. }
  325. a = snap.Availability("b")
  326. if len(a) != 1 || a[0] != remoteDevice0 {
  327. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  328. }
  329. a = snap.Availability("d")
  330. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  331. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  332. }
  333. snap.Release()
  334. // Now bring another remote into play
  335. secRemote := fileList{
  336. local1[0], // a
  337. remote1[0], // b
  338. local1[3], // d
  339. remote1[1], // e
  340. local1[4], // z
  341. }
  342. secRemote[0].Version = secRemote[0].Version.Update(remoteDevice1.Short())
  343. secRemote[1].Version = secRemote[1].Version.Update(remoteDevice1.Short())
  344. secRemote[4].Version = secRemote[4].Version.Update(remoteDevice1.Short())
  345. secRemote[4].Deleted = false
  346. secRemote[4].Blocks = genBlocks(1)
  347. setSequence(0, secRemote)
  348. expectedGlobal = fileList{
  349. secRemote[0], // a
  350. secRemote[1], // b
  351. remote0[2], // c
  352. localTot[3], // d
  353. secRemote[3], // e
  354. secRemote[4], // z
  355. }
  356. expectedLocalNeed = fileList{
  357. secRemote[0], // a
  358. secRemote[1], // b
  359. remote0[2], // c
  360. secRemote[3], // e
  361. secRemote[4], // z
  362. }
  363. expectedRemoteNeed = fileList{
  364. secRemote[0], // a
  365. secRemote[1], // b
  366. local0[3], // d
  367. secRemote[4], // z
  368. }
  369. expectedSecRemoteNeed := fileList{
  370. remote0[2], // c
  371. }
  372. m.Update(remoteDevice1, secRemote)
  373. check()
  374. h := fileList(haveList(t, m, remoteDevice1))
  375. sort.Sort(h)
  376. if fmt.Sprint(h) != fmt.Sprint(secRemote) {
  377. t.Errorf("Have incorrect (secRemote);\n A: %v !=\n E: %v", h, secRemote)
  378. }
  379. n := fileList(needList(t, m, remoteDevice1))
  380. sort.Sort(n)
  381. if fmt.Sprint(n) != fmt.Sprint(expectedSecRemoteNeed) {
  382. t.Errorf("Need incorrect (secRemote);\n A: %v !=\n E: %v", n, expectedSecRemoteNeed)
  383. }
  384. checkNeed(t, m, remoteDevice1, expectedSecRemoteNeed)
  385. }
  386. func TestNeedWithInvalid(t *testing.T) {
  387. ldb := newLowlevelMemory(t)
  388. defer ldb.Close()
  389. s := newFileSet(t, "test", ldb)
  390. localHave := fileList{
  391. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  392. }
  393. remote0Have := fileList{
  394. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  395. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  396. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  397. }
  398. remote1Have := fileList{
  399. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  400. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  401. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  402. }
  403. expectedNeed := fileList{
  404. remote0Have[0],
  405. remote1Have[0],
  406. remote0Have[2],
  407. }
  408. replace(s, protocol.LocalDeviceID, localHave)
  409. replace(s, remoteDevice0, remote0Have)
  410. replace(s, remoteDevice1, remote1Have)
  411. need := fileList(needList(t, s, protocol.LocalDeviceID))
  412. sort.Sort(need)
  413. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  414. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  415. }
  416. checkNeed(t, s, protocol.LocalDeviceID, expectedNeed)
  417. }
  418. func TestUpdateToInvalid(t *testing.T) {
  419. ldb := newLowlevelMemory(t)
  420. defer ldb.Close()
  421. folder := "test"
  422. s := newFileSet(t, folder, ldb)
  423. f := db.NewBlockFinder(ldb)
  424. localHave := fileList{
  425. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1), Size: 1},
  426. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2), Size: 1},
  427. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), LocalFlags: protocol.FlagLocalIgnored, Size: 1},
  428. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7), Size: 1},
  429. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, LocalFlags: protocol.FlagLocalIgnored, Size: 1},
  430. }
  431. replace(s, protocol.LocalDeviceID, localHave)
  432. have := fileList(haveList(t, s, protocol.LocalDeviceID))
  433. sort.Sort(have)
  434. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  435. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  436. }
  437. oldBlockHash := localHave[1].Blocks[0].Hash
  438. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  439. localHave[1].Blocks = nil
  440. localHave[4].LocalFlags = 0
  441. localHave[4].Blocks = genBlocks(3)
  442. s.Update(protocol.LocalDeviceID, append(fileList{}, localHave[1], localHave[4]))
  443. have = fileList(haveList(t, s, protocol.LocalDeviceID))
  444. sort.Sort(have)
  445. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  446. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  447. }
  448. f.Iterate([]string{folder}, oldBlockHash, func(folder, file string, index int32) bool {
  449. if file == localHave[1].Name {
  450. t.Errorf("Found unexpected block in blockmap for invalidated file")
  451. return true
  452. }
  453. return false
  454. })
  455. if !f.Iterate([]string{folder}, localHave[4].Blocks[0].Hash, func(folder, file string, index int32) bool {
  456. return file == localHave[4].Name
  457. }) {
  458. t.Errorf("First block of un-invalidated file is missing from blockmap")
  459. }
  460. }
  461. func TestInvalidAvailability(t *testing.T) {
  462. ldb := newLowlevelMemory(t)
  463. defer ldb.Close()
  464. s := newFileSet(t, "test", ldb)
  465. remote0Have := fileList{
  466. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  467. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  468. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  469. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  470. }
  471. remote1Have := fileList{
  472. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  473. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  474. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  475. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  476. }
  477. replace(s, remoteDevice0, remote0Have)
  478. replace(s, remoteDevice1, remote1Have)
  479. snap := snapshot(t, s)
  480. defer snap.Release()
  481. if av := snap.Availability("both"); len(av) != 2 {
  482. t.Error("Incorrect availability for 'both':", av)
  483. }
  484. if av := snap.Availability("r0only"); len(av) != 1 || av[0] != remoteDevice0 {
  485. t.Error("Incorrect availability for 'r0only':", av)
  486. }
  487. if av := snap.Availability("r1only"); len(av) != 1 || av[0] != remoteDevice1 {
  488. t.Error("Incorrect availability for 'r1only':", av)
  489. }
  490. if av := snap.Availability("none"); len(av) != 0 {
  491. t.Error("Incorrect availability for 'none':", av)
  492. }
  493. }
  494. func TestGlobalReset(t *testing.T) {
  495. ldb := newLowlevelMemory(t)
  496. defer ldb.Close()
  497. m := newFileSet(t, "test", ldb)
  498. local := []protocol.FileInfo{
  499. {Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  500. {Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  501. {Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  502. {Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  503. }
  504. remote := []protocol.FileInfo{
  505. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  506. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  507. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  508. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  509. }
  510. replace(m, protocol.LocalDeviceID, local)
  511. g := globalList(t, m)
  512. sort.Sort(fileList(g))
  513. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  514. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  515. }
  516. replace(m, remoteDevice0, remote)
  517. replace(m, remoteDevice0, nil)
  518. g = globalList(t, m)
  519. sort.Sort(fileList(g))
  520. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  521. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  522. }
  523. }
  524. func TestNeed(t *testing.T) {
  525. ldb := newLowlevelMemory(t)
  526. defer ldb.Close()
  527. m := newFileSet(t, "test", ldb)
  528. local := []protocol.FileInfo{
  529. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  530. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  531. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  532. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  533. }
  534. remote := []protocol.FileInfo{
  535. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  536. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  537. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  538. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  539. }
  540. shouldNeed := []protocol.FileInfo{
  541. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  542. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  543. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  544. }
  545. replace(m, protocol.LocalDeviceID, local)
  546. replace(m, remoteDevice0, remote)
  547. need := needList(t, m, protocol.LocalDeviceID)
  548. sort.Sort(fileList(need))
  549. sort.Sort(fileList(shouldNeed))
  550. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  551. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  552. }
  553. checkNeed(t, m, protocol.LocalDeviceID, shouldNeed)
  554. }
  555. func TestSequence(t *testing.T) {
  556. ldb := newLowlevelMemory(t)
  557. defer ldb.Close()
  558. m := newFileSet(t, "test", ldb)
  559. local1 := []protocol.FileInfo{
  560. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  561. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  562. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  563. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  564. }
  565. local2 := []protocol.FileInfo{
  566. local1[0],
  567. // [1] deleted
  568. local1[2],
  569. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  570. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  571. }
  572. replace(m, protocol.LocalDeviceID, local1)
  573. c0 := m.Sequence(protocol.LocalDeviceID)
  574. replace(m, protocol.LocalDeviceID, local2)
  575. c1 := m.Sequence(protocol.LocalDeviceID)
  576. if !(c1 > c0) {
  577. t.Fatal("Local version number should have incremented")
  578. }
  579. }
  580. func TestListDropFolder(t *testing.T) {
  581. ldb := newLowlevelMemory(t)
  582. defer ldb.Close()
  583. s0 := newFileSet(t, "test0", ldb)
  584. local1 := []protocol.FileInfo{
  585. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  586. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  587. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  588. }
  589. replace(s0, protocol.LocalDeviceID, local1)
  590. s1 := newFileSet(t, "test1", ldb)
  591. local2 := []protocol.FileInfo{
  592. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  593. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  594. {Name: "f", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  595. }
  596. replace(s1, remoteDevice0, local2)
  597. // Check that we have both folders and their data is in the global list
  598. expectedFolderList := []string{"test0", "test1"}
  599. actualFolderList := ldb.ListFolders()
  600. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  601. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  602. }
  603. if l := len(globalList(t, s0)); l != 3 {
  604. t.Errorf("Incorrect global length %d != 3 for s0", l)
  605. }
  606. if l := len(globalList(t, s1)); l != 3 {
  607. t.Errorf("Incorrect global length %d != 3 for s1", l)
  608. }
  609. // Drop one of them and check that it's gone.
  610. db.DropFolder(ldb, "test1")
  611. expectedFolderList = []string{"test0"}
  612. actualFolderList = ldb.ListFolders()
  613. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  614. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  615. }
  616. if l := len(globalList(t, s0)); l != 3 {
  617. t.Errorf("Incorrect global length %d != 3 for s0", l)
  618. }
  619. if l := len(globalList(t, s1)); l != 0 {
  620. t.Errorf("Incorrect global length %d != 0 for s1", l)
  621. }
  622. }
  623. func TestGlobalNeedWithInvalid(t *testing.T) {
  624. ldb := newLowlevelMemory(t)
  625. defer ldb.Close()
  626. s := newFileSet(t, "test1", ldb)
  627. rem0 := fileList{
  628. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  629. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true},
  630. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  631. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  632. }
  633. replace(s, remoteDevice0, rem0)
  634. rem1 := fileList{
  635. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  636. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  637. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true},
  638. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true, ModifiedS: 10},
  639. }
  640. replace(s, remoteDevice1, rem1)
  641. total := fileList{
  642. // There's a valid copy of each file, so it should be merged
  643. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  644. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  645. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  646. // in conflict and older, but still wins as the other is invalid
  647. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  648. }
  649. need := fileList(needList(t, s, protocol.LocalDeviceID))
  650. if fmt.Sprint(need) != fmt.Sprint(total) {
  651. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
  652. }
  653. checkNeed(t, s, protocol.LocalDeviceID, total)
  654. global := fileList(globalList(t, s))
  655. if fmt.Sprint(global) != fmt.Sprint(total) {
  656. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
  657. }
  658. }
  659. func TestLongPath(t *testing.T) {
  660. ldb := newLowlevelMemory(t)
  661. defer ldb.Close()
  662. s := newFileSet(t, "test", ldb)
  663. var b bytes.Buffer
  664. for i := 0; i < 100; i++ {
  665. b.WriteString("012345678901234567890123456789012345678901234567890")
  666. }
  667. name := b.String() // 5000 characters
  668. local := []protocol.FileInfo{
  669. {Name: name, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  670. }
  671. replace(s, protocol.LocalDeviceID, local)
  672. gf := globalList(t, s)
  673. if l := len(gf); l != 1 {
  674. t.Fatalf("Incorrect len %d != 1 for global list", l)
  675. }
  676. if gf[0].Name != local[0].Name {
  677. t.Errorf("Incorrect long filename;\n%q !=\n%q",
  678. gf[0].Name, local[0].Name)
  679. }
  680. }
  681. func BenchmarkUpdateOneFile(b *testing.B) {
  682. local0 := fileList{
  683. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  684. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  685. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  686. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  687. // A longer name is more realistic and causes more allocations
  688. protocol.FileInfo{Name: "zajksdhaskjdh/askjdhaskjdashkajshd/kasjdhaskjdhaskdjhaskdjash/dkjashdaksjdhaskdjahskdjh", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  689. }
  690. be, err := backend.Open("testdata/benchmarkupdate.db", backend.TuningAuto)
  691. if err != nil {
  692. b.Fatal(err)
  693. }
  694. ldb := newLowlevel(b, be)
  695. defer func() {
  696. ldb.Close()
  697. os.RemoveAll("testdata/benchmarkupdate.db")
  698. }()
  699. m := newFileSet(b, "test", ldb)
  700. replace(m, protocol.LocalDeviceID, local0)
  701. l := local0[4:5]
  702. for i := 0; i < b.N; i++ {
  703. l[0].Version = l[0].Version.Update(myID)
  704. m.Update(protocol.LocalDeviceID, local0)
  705. }
  706. b.ReportAllocs()
  707. }
  708. func TestIndexID(t *testing.T) {
  709. ldb := newLowlevelMemory(t)
  710. defer ldb.Close()
  711. s := newFileSet(t, "test", ldb)
  712. // The Index ID for some random device is zero by default.
  713. id := s.IndexID(remoteDevice0)
  714. if id != 0 {
  715. t.Errorf("index ID for remote device should default to zero, not %d", id)
  716. }
  717. // The Index ID for someone else should be settable
  718. s.SetIndexID(remoteDevice0, 42)
  719. id = s.IndexID(remoteDevice0)
  720. if id != 42 {
  721. t.Errorf("index ID for remote device should be remembered; got %d, expected %d", id, 42)
  722. }
  723. // Our own index ID should be generated randomly.
  724. id = s.IndexID(protocol.LocalDeviceID)
  725. if id == 0 {
  726. t.Errorf("index ID for local device should be random, not zero")
  727. }
  728. t.Logf("random index ID is 0x%016x", id)
  729. // But of course always the same after that.
  730. again := s.IndexID(protocol.LocalDeviceID)
  731. if again != id {
  732. t.Errorf("index ID changed; %d != %d", again, id)
  733. }
  734. }
  735. func TestDropFiles(t *testing.T) {
  736. ldb := newLowlevelMemory(t)
  737. m := newFileSet(t, "test", ldb)
  738. local0 := fileList{
  739. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  740. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  741. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  742. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  743. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  744. }
  745. remote0 := fileList{
  746. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  747. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  748. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  749. }
  750. // Insert files
  751. m.Update(protocol.LocalDeviceID, local0)
  752. m.Update(remoteDevice0, remote0)
  753. // Check that they're there
  754. h := haveList(t, m, protocol.LocalDeviceID)
  755. if len(h) != len(local0) {
  756. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  757. }
  758. h = haveList(t, m, remoteDevice0)
  759. if len(h) != len(remote0) {
  760. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  761. }
  762. g := globalList(t, m)
  763. if len(g) != len(local0) {
  764. // local0 covers all files
  765. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(local0))
  766. }
  767. // Drop the local files and recheck
  768. m.Drop(protocol.LocalDeviceID)
  769. h = haveList(t, m, protocol.LocalDeviceID)
  770. if len(h) != 0 {
  771. t.Errorf("Incorrect number of files after drop, %d != %d", len(h), 0)
  772. }
  773. h = haveList(t, m, remoteDevice0)
  774. if len(h) != len(remote0) {
  775. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  776. }
  777. g = globalList(t, m)
  778. if len(g) != len(remote0) {
  779. // the ones in remote0 remain
  780. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(remote0))
  781. }
  782. }
  783. func TestIssue4701(t *testing.T) {
  784. ldb := newLowlevelMemory(t)
  785. defer ldb.Close()
  786. s := newFileSet(t, "test", ldb)
  787. localHave := fileList{
  788. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  789. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, LocalFlags: protocol.FlagLocalIgnored},
  790. }
  791. s.Update(protocol.LocalDeviceID, localHave)
  792. if c := localSize(t, s); c.Files != 1 {
  793. t.Errorf("Expected 1 local file, got %v", c.Files)
  794. }
  795. if c := globalSize(t, s); c.Files != 1 {
  796. t.Errorf("Expected 1 global file, got %v", c.Files)
  797. }
  798. localHave[1].LocalFlags = 0
  799. s.Update(protocol.LocalDeviceID, localHave)
  800. if c := localSize(t, s); c.Files != 2 {
  801. t.Errorf("Expected 2 local files, got %v", c.Files)
  802. }
  803. if c := globalSize(t, s); c.Files != 2 {
  804. t.Errorf("Expected 2 global files, got %v", c.Files)
  805. }
  806. localHave[0].LocalFlags = protocol.FlagLocalIgnored
  807. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  808. s.Update(protocol.LocalDeviceID, localHave)
  809. if c := localSize(t, s); c.Files != 0 {
  810. t.Errorf("Expected 0 local files, got %v", c.Files)
  811. }
  812. if c := globalSize(t, s); c.Files != 0 {
  813. t.Errorf("Expected 0 global files, got %v", c.Files)
  814. }
  815. }
  816. func TestWithHaveSequence(t *testing.T) {
  817. ldb := newLowlevelMemory(t)
  818. defer ldb.Close()
  819. folder := "test"
  820. s := newFileSet(t, folder, ldb)
  821. // The files must not be in alphabetical order
  822. localHave := fileList{
  823. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, RawInvalid: true},
  824. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  825. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  826. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  827. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  828. }
  829. replace(s, protocol.LocalDeviceID, localHave)
  830. i := 2
  831. snap := snapshot(t, s)
  832. defer snap.Release()
  833. snap.WithHaveSequence(int64(i), func(fi protocol.FileIntf) bool {
  834. if f := fi.(protocol.FileInfo); !f.IsEquivalent(localHave[i-1], 0) {
  835. t.Fatalf("Got %v\nExpected %v", f, localHave[i-1])
  836. }
  837. i++
  838. return true
  839. })
  840. }
  841. func TestStressWithHaveSequence(t *testing.T) {
  842. // This races two loops against each other: one that continuously does
  843. // updates, and one that continuously does sequence walks. The test fails
  844. // if the sequence walker sees a discontinuity.
  845. if testing.Short() {
  846. t.Skip("Takes a long time")
  847. }
  848. ldb := newLowlevelMemory(t)
  849. defer ldb.Close()
  850. folder := "test"
  851. s := newFileSet(t, folder, ldb)
  852. var localHave []protocol.FileInfo
  853. for i := 0; i < 100; i++ {
  854. localHave = append(localHave, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Blocks: genBlocks(i * 10)})
  855. }
  856. done := make(chan struct{})
  857. t0 := time.Now()
  858. go func() {
  859. for time.Since(t0) < 10*time.Second {
  860. for j, f := range localHave {
  861. localHave[j].Version = f.Version.Update(42)
  862. }
  863. s.Update(protocol.LocalDeviceID, localHave)
  864. }
  865. close(done)
  866. }()
  867. var prevSeq int64
  868. loop:
  869. for {
  870. select {
  871. case <-done:
  872. break loop
  873. default:
  874. }
  875. snap := snapshot(t, s)
  876. snap.WithHaveSequence(prevSeq+1, func(fi protocol.FileIntf) bool {
  877. if fi.SequenceNo() < prevSeq+1 {
  878. t.Fatal("Skipped ", prevSeq+1, fi.SequenceNo())
  879. }
  880. prevSeq = fi.SequenceNo()
  881. return true
  882. })
  883. snap.Release()
  884. }
  885. }
  886. func TestIssue4925(t *testing.T) {
  887. ldb := newLowlevelMemory(t)
  888. defer ldb.Close()
  889. folder := "test"
  890. s := newFileSet(t, folder, ldb)
  891. localHave := fileList{
  892. protocol.FileInfo{Name: "dir"},
  893. protocol.FileInfo{Name: "dir.file"},
  894. protocol.FileInfo{Name: "dir/file"},
  895. }
  896. replace(s, protocol.LocalDeviceID, localHave)
  897. for _, prefix := range []string{"dir", "dir/"} {
  898. pl := haveListPrefixed(t, s, protocol.LocalDeviceID, prefix)
  899. if l := len(pl); l != 2 {
  900. t.Errorf("Expected 2, got %v local items below %v", l, prefix)
  901. }
  902. pl = globalListPrefixed(t, s, prefix)
  903. if l := len(pl); l != 2 {
  904. t.Errorf("Expected 2, got %v global items below %v", l, prefix)
  905. }
  906. }
  907. }
  908. func TestMoveGlobalBack(t *testing.T) {
  909. ldb := newLowlevelMemory(t)
  910. defer ldb.Close()
  911. folder := "test"
  912. file := "foo"
  913. s := newFileSet(t, folder, ldb)
  914. localHave := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Blocks: genBlocks(1), ModifiedS: 10, Size: 1}}
  915. remote0Have := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}, {ID: remoteDevice0.Short(), Value: 1}}}, Blocks: genBlocks(2), ModifiedS: 0, Size: 2}}
  916. s.Update(protocol.LocalDeviceID, localHave)
  917. s.Update(remoteDevice0, remote0Have)
  918. if need := needList(t, s, protocol.LocalDeviceID); len(need) != 1 {
  919. t.Error("Expected 1 local need, got", need)
  920. } else if !need[0].IsEquivalent(remote0Have[0], 0) {
  921. t.Errorf("Local need incorrect;\n A: %v !=\n E: %v", need[0], remote0Have[0])
  922. }
  923. checkNeed(t, s, protocol.LocalDeviceID, remote0Have[:1])
  924. if need := needList(t, s, remoteDevice0); len(need) != 0 {
  925. t.Error("Expected no need for remote 0, got", need)
  926. }
  927. checkNeed(t, s, remoteDevice0, nil)
  928. ls := localSize(t, s)
  929. if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
  930. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  931. }
  932. gs := globalSize(t, s)
  933. if globalBytes := remote0Have[0].Size; gs.Bytes != globalBytes {
  934. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  935. }
  936. // That's what happens when something becomes unignored or something.
  937. // In any case it will be moved back from first spot in the global list
  938. // which is the scenario to be tested here.
  939. remote0Have[0].Version = remote0Have[0].Version.Update(remoteDevice0.Short()).DropOthers(remoteDevice0.Short())
  940. s.Update(remoteDevice0, remote0Have)
  941. if need := needList(t, s, remoteDevice0); len(need) != 1 {
  942. t.Error("Expected 1 need for remote 0, got", need)
  943. } else if !need[0].IsEquivalent(localHave[0], 0) {
  944. t.Errorf("Need for remote 0 incorrect;\n A: %v !=\n E: %v", need[0], localHave[0])
  945. }
  946. checkNeed(t, s, remoteDevice0, localHave[:1])
  947. if need := needList(t, s, protocol.LocalDeviceID); len(need) != 0 {
  948. t.Error("Expected no local need, got", need)
  949. }
  950. checkNeed(t, s, protocol.LocalDeviceID, nil)
  951. ls = localSize(t, s)
  952. if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
  953. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  954. }
  955. gs = globalSize(t, s)
  956. if globalBytes := localHave[0].Size; gs.Bytes != globalBytes {
  957. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  958. }
  959. }
  960. // TestIssue5007 checks, that updating the local device with an invalid file
  961. // info with the newest version does indeed remove that file from the list of
  962. // needed files.
  963. // https://github.com/syncthing/syncthing/issues/5007
  964. func TestIssue5007(t *testing.T) {
  965. ldb := newLowlevelMemory(t)
  966. defer ldb.Close()
  967. folder := "test"
  968. file := "foo"
  969. s := newFileSet(t, folder, ldb)
  970. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}}}
  971. s.Update(remoteDevice0, fs)
  972. if need := needList(t, s, protocol.LocalDeviceID); len(need) != 1 {
  973. t.Fatal("Expected 1 local need, got", need)
  974. } else if !need[0].IsEquivalent(fs[0], 0) {
  975. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  976. }
  977. checkNeed(t, s, protocol.LocalDeviceID, fs[:1])
  978. fs[0].LocalFlags = protocol.FlagLocalIgnored
  979. s.Update(protocol.LocalDeviceID, fs)
  980. if need := needList(t, s, protocol.LocalDeviceID); len(need) != 0 {
  981. t.Fatal("Expected no local need, got", need)
  982. }
  983. checkNeed(t, s, protocol.LocalDeviceID, nil)
  984. }
  985. // TestNeedDeleted checks that a file that doesn't exist locally isn't needed
  986. // when the global file is deleted.
  987. func TestNeedDeleted(t *testing.T) {
  988. ldb := newLowlevelMemory(t)
  989. defer ldb.Close()
  990. folder := "test"
  991. file := "foo"
  992. s := newFileSet(t, folder, ldb)
  993. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Deleted: true}}
  994. s.Update(remoteDevice0, fs)
  995. if need := needList(t, s, protocol.LocalDeviceID); len(need) != 0 {
  996. t.Fatal("Expected no local need, got", need)
  997. }
  998. checkNeed(t, s, protocol.LocalDeviceID, nil)
  999. fs[0].Deleted = false
  1000. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  1001. s.Update(remoteDevice0, fs)
  1002. if need := needList(t, s, protocol.LocalDeviceID); len(need) != 1 {
  1003. t.Fatal("Expected 1 local need, got", need)
  1004. } else if !need[0].IsEquivalent(fs[0], 0) {
  1005. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  1006. }
  1007. checkNeed(t, s, protocol.LocalDeviceID, fs[:1])
  1008. fs[0].Deleted = true
  1009. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  1010. s.Update(remoteDevice0, fs)
  1011. if need := needList(t, s, protocol.LocalDeviceID); len(need) != 0 {
  1012. t.Fatal("Expected no local need, got", need)
  1013. }
  1014. checkNeed(t, s, protocol.LocalDeviceID, nil)
  1015. }
  1016. func TestReceiveOnlyAccounting(t *testing.T) {
  1017. ldb := newLowlevelMemory(t)
  1018. defer ldb.Close()
  1019. folder := "test"
  1020. s := newFileSet(t, folder, ldb)
  1021. local := protocol.DeviceID{1}
  1022. remote := protocol.DeviceID{2}
  1023. // Three files that have been created by the remote device
  1024. version := protocol.Vector{Counters: []protocol.Counter{{ID: remote.Short(), Value: 1}}}
  1025. files := fileList{
  1026. protocol.FileInfo{Name: "f1", Size: 10, Sequence: 1, Version: version},
  1027. protocol.FileInfo{Name: "f2", Size: 10, Sequence: 1, Version: version},
  1028. protocol.FileInfo{Name: "f3", Size: 10, Sequence: 1, Version: version},
  1029. }
  1030. // We have synced them locally
  1031. replace(s, protocol.LocalDeviceID, files)
  1032. replace(s, remote, files)
  1033. if n := localSize(t, s).Files; n != 3 {
  1034. t.Fatal("expected 3 local files initially, not", n)
  1035. }
  1036. if n := localSize(t, s).Bytes; n != 30 {
  1037. t.Fatal("expected 30 local bytes initially, not", n)
  1038. }
  1039. if n := globalSize(t, s).Files; n != 3 {
  1040. t.Fatal("expected 3 global files initially, not", n)
  1041. }
  1042. if n := globalSize(t, s).Bytes; n != 30 {
  1043. t.Fatal("expected 30 global bytes initially, not", n)
  1044. }
  1045. if n := receiveOnlyChangedSize(t, s).Files; n != 0 {
  1046. t.Fatal("expected 0 receive only changed files initially, not", n)
  1047. }
  1048. if n := receiveOnlyChangedSize(t, s).Bytes; n != 0 {
  1049. t.Fatal("expected 0 receive only changed bytes initially, not", n)
  1050. }
  1051. // Detected a local change in a receive only folder
  1052. changed := files[0]
  1053. changed.Version = changed.Version.Update(local.Short())
  1054. changed.Size = 100
  1055. changed.ModifiedBy = local.Short()
  1056. changed.LocalFlags = protocol.FlagLocalReceiveOnly
  1057. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{changed})
  1058. // Check that we see the files
  1059. if n := localSize(t, s).Files; n != 3 {
  1060. t.Fatal("expected 3 local files after local change, not", n)
  1061. }
  1062. if n := localSize(t, s).Bytes; n != 120 {
  1063. t.Fatal("expected 120 local bytes after local change, not", n)
  1064. }
  1065. if n := globalSize(t, s).Files; n != 3 {
  1066. t.Fatal("expected 3 global files after local change, not", n)
  1067. }
  1068. if n := globalSize(t, s).Bytes; n != 30 {
  1069. t.Fatal("expected 30 global files after local change, not", n)
  1070. }
  1071. if n := receiveOnlyChangedSize(t, s).Files; n != 1 {
  1072. t.Fatal("expected 1 receive only changed file after local change, not", n)
  1073. }
  1074. if n := receiveOnlyChangedSize(t, s).Bytes; n != 100 {
  1075. t.Fatal("expected 100 receive only changed bytes after local change, not", n)
  1076. }
  1077. // Fake a revert. That's a two step process, first converting our
  1078. // changed file into a less preferred variant, then pulling down the old
  1079. // version.
  1080. changed.Version = protocol.Vector{}
  1081. changed.LocalFlags &^= protocol.FlagLocalReceiveOnly
  1082. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{changed})
  1083. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{files[0]})
  1084. // Check that we see the files, same data as initially
  1085. if n := localSize(t, s).Files; n != 3 {
  1086. t.Fatal("expected 3 local files after revert, not", n)
  1087. }
  1088. if n := localSize(t, s).Bytes; n != 30 {
  1089. t.Fatal("expected 30 local bytes after revert, not", n)
  1090. }
  1091. if n := globalSize(t, s).Files; n != 3 {
  1092. t.Fatal("expected 3 global files after revert, not", n)
  1093. }
  1094. if n := globalSize(t, s).Bytes; n != 30 {
  1095. t.Fatal("expected 30 global bytes after revert, not", n)
  1096. }
  1097. if n := receiveOnlyChangedSize(t, s).Files; n != 0 {
  1098. t.Fatal("expected 0 receive only changed files after revert, not", n)
  1099. }
  1100. if n := receiveOnlyChangedSize(t, s).Bytes; n != 0 {
  1101. t.Fatal("expected 0 receive only changed bytes after revert, not", n)
  1102. }
  1103. }
  1104. func TestNeedAfterUnignore(t *testing.T) {
  1105. ldb := newLowlevelMemory(t)
  1106. defer ldb.Close()
  1107. folder := "test"
  1108. file := "foo"
  1109. s := newFileSet(t, folder, ldb)
  1110. remID := remoteDevice0.Short()
  1111. // Initial state: Devices in sync, locally ignored
  1112. local := protocol.FileInfo{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: remID, Value: 1}, {ID: myID, Value: 1}}}, ModifiedS: 10}
  1113. local.SetIgnored()
  1114. remote := protocol.FileInfo{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: remID, Value: 1}, {ID: myID, Value: 1}}}, ModifiedS: 10}
  1115. s.Update(protocol.LocalDeviceID, fileList{local})
  1116. s.Update(remoteDevice0, fileList{remote})
  1117. // Unignore locally -> conflicting changes. Remote is newer, thus winning.
  1118. local.Version = local.Version.Update(myID)
  1119. local.Version = local.Version.DropOthers(myID)
  1120. local.LocalFlags = 0
  1121. local.ModifiedS = 0
  1122. s.Update(protocol.LocalDeviceID, fileList{local})
  1123. if need := needList(t, s, protocol.LocalDeviceID); len(need) != 1 {
  1124. t.Fatal("Expected one local need, got", need)
  1125. } else if !need[0].IsEquivalent(remote, 0) {
  1126. t.Fatalf("Got %v, expected %v", need[0], remote)
  1127. }
  1128. checkNeed(t, s, protocol.LocalDeviceID, []protocol.FileInfo{remote})
  1129. }
  1130. func TestRemoteInvalidNotAccounted(t *testing.T) {
  1131. // Remote files with the invalid bit should not count.
  1132. ldb := newLowlevelMemory(t)
  1133. defer ldb.Close()
  1134. s := newFileSet(t, "test", ldb)
  1135. files := []protocol.FileInfo{
  1136. {Name: "a", Size: 1234, Sequence: 42, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}}, // valid, should count
  1137. {Name: "b", Size: 1234, Sequence: 43, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, RawInvalid: true}, // invalid, doesn't count
  1138. }
  1139. s.Update(remoteDevice0, files)
  1140. global := globalSize(t, s)
  1141. if global.Files != 1 {
  1142. t.Error("Expected one file in global size, not", global.Files)
  1143. }
  1144. if global.Bytes != 1234 {
  1145. t.Error("Expected 1234 bytes in global size, not", global.Bytes)
  1146. }
  1147. }
  1148. func TestNeedWithNewerInvalid(t *testing.T) {
  1149. ldb := newLowlevelMemory(t)
  1150. defer ldb.Close()
  1151. s := newFileSet(t, "default", ldb)
  1152. rem0ID := remoteDevice0.Short()
  1153. rem1ID := remoteDevice1.Short()
  1154. // Initial state: file present on rem0 and rem1, but not locally.
  1155. file := protocol.FileInfo{Name: "foo"}
  1156. file.Version = file.Version.Update(rem0ID)
  1157. s.Update(remoteDevice0, fileList{file})
  1158. s.Update(remoteDevice1, fileList{file})
  1159. need := needList(t, s, protocol.LocalDeviceID)
  1160. if len(need) != 1 {
  1161. t.Fatal("Locally missing file should be needed")
  1162. }
  1163. if !need[0].IsEquivalent(file, 0) {
  1164. t.Fatalf("Got needed file %v, expected %v", need[0], file)
  1165. }
  1166. checkNeed(t, s, protocol.LocalDeviceID, []protocol.FileInfo{file})
  1167. // rem1 sends an invalid file with increased version
  1168. inv := file
  1169. inv.Version = inv.Version.Update(rem1ID)
  1170. inv.RawInvalid = true
  1171. s.Update(remoteDevice1, fileList{inv})
  1172. // We still have an old file, we need the newest valid file
  1173. need = needList(t, s, protocol.LocalDeviceID)
  1174. if len(need) != 1 {
  1175. t.Fatal("Locally missing file should be needed regardless of invalid files")
  1176. }
  1177. if !need[0].IsEquivalent(file, 0) {
  1178. t.Fatalf("Got needed file %v, expected %v", need[0], file)
  1179. }
  1180. checkNeed(t, s, protocol.LocalDeviceID, []protocol.FileInfo{file})
  1181. }
  1182. func TestNeedAfterDeviceRemove(t *testing.T) {
  1183. ldb := newLowlevelMemory(t)
  1184. defer ldb.Close()
  1185. file := "foo"
  1186. s := newFileSet(t, "test", ldb)
  1187. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}}}
  1188. s.Update(protocol.LocalDeviceID, fs)
  1189. fs[0].Version = fs[0].Version.Update(myID)
  1190. s.Update(remoteDevice0, fs)
  1191. if need := needList(t, s, protocol.LocalDeviceID); len(need) != 1 {
  1192. t.Fatal("Expected one local need, got", need)
  1193. }
  1194. s.Drop(remoteDevice0)
  1195. if need := needList(t, s, protocol.LocalDeviceID); len(need) != 0 {
  1196. t.Fatal("Expected no local need, got", need)
  1197. }
  1198. checkNeed(t, s, protocol.LocalDeviceID, nil)
  1199. }
  1200. func TestCaseSensitive(t *testing.T) {
  1201. // Normal case sensitive lookup should work
  1202. ldb := newLowlevelMemory(t)
  1203. defer ldb.Close()
  1204. s := newFileSet(t, "test", ldb)
  1205. local := []protocol.FileInfo{
  1206. {Name: filepath.FromSlash("D1/f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1207. {Name: filepath.FromSlash("F1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1208. {Name: filepath.FromSlash("d1/F1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1209. {Name: filepath.FromSlash("d1/f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1210. {Name: filepath.FromSlash("f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1211. }
  1212. replace(s, protocol.LocalDeviceID, local)
  1213. gf := globalList(t, s)
  1214. if l := len(gf); l != len(local) {
  1215. t.Fatalf("Incorrect len %d != %d for global list", l, len(local))
  1216. }
  1217. for i := range local {
  1218. if gf[i].Name != local[i].Name {
  1219. t.Errorf("Incorrect filename;\n%q !=\n%q",
  1220. gf[i].Name, local[i].Name)
  1221. }
  1222. }
  1223. }
  1224. func TestSequenceIndex(t *testing.T) {
  1225. // This test attempts to verify correct operation of the sequence index.
  1226. // It's a stress test and needs to run for a long time, but we don't
  1227. // really have time for that in normal builds.
  1228. runtime := time.Minute
  1229. if testing.Short() {
  1230. runtime = time.Second
  1231. }
  1232. // Set up a db and a few files that we will manipulate.
  1233. ldb := newLowlevelMemory(t)
  1234. defer ldb.Close()
  1235. s := newFileSet(t, "test", ldb)
  1236. local := []protocol.FileInfo{
  1237. {Name: filepath.FromSlash("banana"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1238. {Name: filepath.FromSlash("pineapple"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1239. {Name: filepath.FromSlash("orange"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1240. {Name: filepath.FromSlash("apple"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1241. {Name: filepath.FromSlash("jackfruit"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1242. }
  1243. // Start a background routine that makes updates to these files as fast
  1244. // as it can. We always update the same files in the same order.
  1245. done := make(chan struct{})
  1246. defer close(done)
  1247. go func() {
  1248. for {
  1249. select {
  1250. case <-done:
  1251. return
  1252. default:
  1253. }
  1254. for i := range local {
  1255. local[i].Version = local[i].Version.Update(42)
  1256. }
  1257. s.Update(protocol.LocalDeviceID, local)
  1258. }
  1259. }()
  1260. // Start a routine to walk the sequence index and inspect the result.
  1261. seen := make(map[string]protocol.FileIntf)
  1262. latest := make([]protocol.FileIntf, 0, len(local))
  1263. var seq int64
  1264. t0 := time.Now()
  1265. for time.Since(t0) < runtime {
  1266. // Walk the changes since our last iteration. This should give is
  1267. // one instance each of the files that are changed all the time, or
  1268. // a subset of those files if we manage to run before a complete
  1269. // update has happened since our last iteration.
  1270. latest = latest[:0]
  1271. snap := snapshot(t, s)
  1272. snap.WithHaveSequence(seq+1, func(f protocol.FileIntf) bool {
  1273. seen[f.FileName()] = f
  1274. latest = append(latest, f)
  1275. seq = f.SequenceNo()
  1276. return true
  1277. })
  1278. snap.Release()
  1279. // Calculate the spread in sequence number.
  1280. var max, min int64
  1281. for _, v := range seen {
  1282. s := v.SequenceNo()
  1283. if max == 0 || max < s {
  1284. max = s
  1285. }
  1286. if min == 0 || min > s {
  1287. min = s
  1288. }
  1289. }
  1290. // We shouldn't see a spread larger than the number of files, as
  1291. // that would mean we have missed updates. For example, if we were
  1292. // to see the following:
  1293. //
  1294. // banana N
  1295. // pineapple N+1
  1296. // orange N+2
  1297. // apple N+10
  1298. // jackfruit N+11
  1299. //
  1300. // that would mean that there have been updates to banana, pineapple
  1301. // and orange that we didn't see in this pass. If those files aren't
  1302. // updated again, those updates are permanently lost.
  1303. if max-min > int64(len(local)) {
  1304. for _, v := range seen {
  1305. t.Log("seen", v.FileName(), v.SequenceNo())
  1306. }
  1307. for _, v := range latest {
  1308. t.Log("latest", v.FileName(), v.SequenceNo())
  1309. }
  1310. t.Fatal("large spread")
  1311. }
  1312. time.Sleep(time.Millisecond)
  1313. }
  1314. }
  1315. func TestIgnoreAfterReceiveOnly(t *testing.T) {
  1316. ldb := newLowlevelMemory(t)
  1317. defer ldb.Close()
  1318. file := "foo"
  1319. s := newFileSet(t, "test", ldb)
  1320. fs := fileList{{
  1321. Name: file,
  1322. Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}},
  1323. LocalFlags: protocol.FlagLocalReceiveOnly,
  1324. }}
  1325. s.Update(protocol.LocalDeviceID, fs)
  1326. fs[0].LocalFlags = protocol.FlagLocalIgnored
  1327. s.Update(protocol.LocalDeviceID, fs)
  1328. snap := snapshot(t, s)
  1329. defer snap.Release()
  1330. if f, ok := snap.Get(protocol.LocalDeviceID, file); !ok {
  1331. t.Error("File missing in db")
  1332. } else if f.IsReceiveOnlyChanged() {
  1333. t.Error("File is still receive-only changed")
  1334. } else if !f.IsIgnored() {
  1335. t.Error("File is not ignored")
  1336. }
  1337. }
  1338. // https://github.com/syncthing/syncthing/issues/6650
  1339. func TestUpdateWithOneFileTwice(t *testing.T) {
  1340. ldb := newLowlevelMemory(t)
  1341. defer ldb.Close()
  1342. file := "foo"
  1343. s := newFileSet(t, "test", ldb)
  1344. fs := fileList{{
  1345. Name: file,
  1346. Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}},
  1347. Sequence: 1,
  1348. }}
  1349. s.Update(protocol.LocalDeviceID, fs)
  1350. fs = append(fs, fs[0])
  1351. for i := range fs {
  1352. fs[i].Sequence++
  1353. fs[i].Version = fs[i].Version.Update(myID)
  1354. }
  1355. fs[1].Sequence++
  1356. fs[1].Version = fs[1].Version.Update(myID)
  1357. s.Update(protocol.LocalDeviceID, fs)
  1358. snap := snapshot(t, s)
  1359. defer snap.Release()
  1360. count := 0
  1361. snap.WithHaveSequence(0, func(f protocol.FileIntf) bool {
  1362. count++
  1363. return true
  1364. })
  1365. if count != 1 {
  1366. t.Error("Expected to have one file, got", count)
  1367. }
  1368. }
  1369. // https://github.com/syncthing/syncthing/issues/6668
  1370. func TestNeedRemoteOnly(t *testing.T) {
  1371. ldb := newLowlevelMemory(t)
  1372. defer ldb.Close()
  1373. s := newFileSet(t, "test", ldb)
  1374. remote0Have := fileList{
  1375. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  1376. }
  1377. s.Update(remoteDevice0, remote0Have)
  1378. need := needSize(t, s, remoteDevice0)
  1379. if !need.Equal(db.Counts{}) {
  1380. t.Error("Expected nothing needed, got", need)
  1381. }
  1382. }
  1383. // https://github.com/syncthing/syncthing/issues/6784
  1384. func TestNeedRemoteAfterReset(t *testing.T) {
  1385. ldb := newLowlevelMemory(t)
  1386. defer ldb.Close()
  1387. s := newFileSet(t, "test", ldb)
  1388. files := fileList{
  1389. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  1390. }
  1391. s.Update(protocol.LocalDeviceID, files)
  1392. s.Update(remoteDevice0, files)
  1393. need := needSize(t, s, remoteDevice0)
  1394. if !need.Equal(db.Counts{}) {
  1395. t.Error("Expected nothing needed, got", need)
  1396. }
  1397. s.Drop(remoteDevice0)
  1398. need = needSize(t, s, remoteDevice0)
  1399. if exp := (db.Counts{Files: 1}); !need.Equal(exp) {
  1400. t.Errorf("Expected %v, got %v", exp, need)
  1401. }
  1402. }
  1403. // https://github.com/syncthing/syncthing/issues/6850
  1404. func TestIgnoreLocalChanged(t *testing.T) {
  1405. ldb := newLowlevelMemory(t)
  1406. defer ldb.Close()
  1407. s := newFileSet(t, "test", ldb)
  1408. // Add locally changed file
  1409. files := fileList{
  1410. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2), LocalFlags: protocol.FlagLocalReceiveOnly},
  1411. }
  1412. s.Update(protocol.LocalDeviceID, files)
  1413. if c := globalSize(t, s).Files; c != 0 {
  1414. t.Error("Expected no global file, got", c)
  1415. }
  1416. if c := localSize(t, s).Files; c != 1 {
  1417. t.Error("Expected one local file, got", c)
  1418. }
  1419. // Change file to ignored
  1420. files[0].LocalFlags = protocol.FlagLocalIgnored
  1421. s.Update(protocol.LocalDeviceID, files)
  1422. if c := globalSize(t, s).Files; c != 0 {
  1423. t.Error("Expected no global file, got", c)
  1424. }
  1425. if c := localSize(t, s).Files; c != 0 {
  1426. t.Error("Expected no local file, got", c)
  1427. }
  1428. }
  1429. // Dropping the index ID on Drop is bad, because Drop gets called when receiving
  1430. // an Index (as opposed to an IndexUpdate), and we don't want to loose the index
  1431. // ID when that happens.
  1432. func TestNoIndexIDResetOnDrop(t *testing.T) {
  1433. ldb := newLowlevelMemory(t)
  1434. defer ldb.Close()
  1435. s := newFileSet(t, "test", ldb)
  1436. s.SetIndexID(remoteDevice0, 1)
  1437. s.Drop(remoteDevice0)
  1438. if got := s.IndexID(remoteDevice0); got != 1 {
  1439. t.Errorf("Expected unchanged (%v), got %v", 1, got)
  1440. }
  1441. }
  1442. func TestConcurrentIndexID(t *testing.T) {
  1443. done := make(chan struct{})
  1444. var ids [2]protocol.IndexID
  1445. setID := func(s *db.FileSet, i int) {
  1446. ids[i] = s.IndexID(protocol.LocalDeviceID)
  1447. done <- struct{}{}
  1448. }
  1449. max := 100
  1450. if testing.Short() {
  1451. max = 10
  1452. }
  1453. for i := 0; i < max; i++ {
  1454. ldb := newLowlevelMemory(t)
  1455. s := newFileSet(t, "test", ldb)
  1456. go setID(s, 0)
  1457. go setID(s, 1)
  1458. <-done
  1459. <-done
  1460. ldb.Close()
  1461. if ids[0] != ids[1] {
  1462. t.Fatalf("IDs differ after %v rounds", i)
  1463. }
  1464. }
  1465. }
  1466. func TestNeedRemoveLastValid(t *testing.T) {
  1467. db := newLowlevelMemory(t)
  1468. defer db.Close()
  1469. folder := "test"
  1470. fs := newFileSet(t, folder, db)
  1471. files := []protocol.FileInfo{
  1472. {Name: "foo", Version: protocol.Vector{}.Update(myID), Sequence: 1},
  1473. }
  1474. fs.Update(remoteDevice0, files)
  1475. files[0].Version = files[0].Version.Update(myID)
  1476. fs.Update(remoteDevice1, files)
  1477. files[0].LocalFlags = protocol.FlagLocalIgnored
  1478. fs.Update(protocol.LocalDeviceID, files)
  1479. snap := snapshot(t, fs)
  1480. c := snap.NeedSize(remoteDevice0)
  1481. if c.Files != 1 {
  1482. t.Errorf("Expected 1 needed files initially, got %v", c.Files)
  1483. }
  1484. snap.Release()
  1485. fs.Drop(remoteDevice1)
  1486. snap = snapshot(t, fs)
  1487. c = snap.NeedSize(remoteDevice0)
  1488. if c.Files != 0 {
  1489. t.Errorf("Expected no needed files, got %v", c.Files)
  1490. }
  1491. snap.Release()
  1492. }
  1493. func replace(fs *db.FileSet, device protocol.DeviceID, files []protocol.FileInfo) {
  1494. fs.Drop(device)
  1495. fs.Update(device, files)
  1496. }
  1497. func localSize(t testing.TB, fs *db.FileSet) db.Counts {
  1498. snap := snapshot(t, fs)
  1499. defer snap.Release()
  1500. return snap.LocalSize()
  1501. }
  1502. func globalSize(t testing.TB, fs *db.FileSet) db.Counts {
  1503. snap := snapshot(t, fs)
  1504. defer snap.Release()
  1505. return snap.GlobalSize()
  1506. }
  1507. func needSize(t testing.TB, fs *db.FileSet, id protocol.DeviceID) db.Counts {
  1508. snap := snapshot(t, fs)
  1509. defer snap.Release()
  1510. return snap.NeedSize(id)
  1511. }
  1512. func receiveOnlyChangedSize(t testing.TB, fs *db.FileSet) db.Counts {
  1513. snap := snapshot(t, fs)
  1514. defer snap.Release()
  1515. return snap.ReceiveOnlyChangedSize()
  1516. }
  1517. func filesToCounts(files []protocol.FileInfo) db.Counts {
  1518. cp := db.Counts{}
  1519. for _, f := range files {
  1520. switch {
  1521. case f.IsDeleted():
  1522. cp.Deleted++
  1523. case f.IsDirectory() && !f.IsSymlink():
  1524. cp.Directories++
  1525. case f.IsSymlink():
  1526. cp.Symlinks++
  1527. default:
  1528. cp.Files++
  1529. }
  1530. cp.Bytes += f.FileSize()
  1531. }
  1532. return cp
  1533. }
  1534. func checkNeed(t testing.TB, s *db.FileSet, dev protocol.DeviceID, expected []protocol.FileInfo) {
  1535. t.Helper()
  1536. counts := needSize(t, s, dev)
  1537. if exp := filesToCounts(expected); !exp.Equal(counts) {
  1538. t.Errorf("Count incorrect (%v): expected %v, got %v", dev, exp, counts)
  1539. }
  1540. }
  1541. func newLowlevel(t testing.TB, backend backend.Backend) *db.Lowlevel {
  1542. t.Helper()
  1543. ll, err := db.NewLowlevel(backend, events.NoopLogger)
  1544. if err != nil {
  1545. t.Fatal(err)
  1546. }
  1547. return ll
  1548. }
  1549. func newLowlevelMemory(t testing.TB) *db.Lowlevel {
  1550. return newLowlevel(t, backend.OpenMemory())
  1551. }
  1552. func newFileSet(t testing.TB, folder string, ll *db.Lowlevel) *db.FileSet {
  1553. t.Helper()
  1554. fset, err := db.NewFileSet(folder, ll)
  1555. if err != nil {
  1556. t.Fatal(err)
  1557. }
  1558. return fset
  1559. }
  1560. func snapshot(t testing.TB, fset *db.FileSet) *db.Snapshot {
  1561. t.Helper()
  1562. snap, err := fset.Snapshot()
  1563. if err != nil {
  1564. t.Fatal(err)
  1565. }
  1566. return snap
  1567. }