model_test.go 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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 model
  7. import (
  8. "bytes"
  9. "encoding/json"
  10. "fmt"
  11. "io/ioutil"
  12. "math/rand"
  13. "os"
  14. "path/filepath"
  15. "strconv"
  16. "testing"
  17. "time"
  18. "github.com/syncthing/protocol"
  19. "github.com/syncthing/syncthing/internal/config"
  20. "github.com/syncthing/syncthing/internal/db"
  21. "github.com/syndtr/goleveldb/leveldb"
  22. "github.com/syndtr/goleveldb/leveldb/storage"
  23. )
  24. var device1, device2 protocol.DeviceID
  25. var defaultConfig *config.Wrapper
  26. var defaultFolderConfig config.FolderConfiguration
  27. func init() {
  28. device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  29. device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
  30. defaultFolderConfig = config.FolderConfiguration{
  31. ID: "default",
  32. RawPath: "testdata",
  33. Devices: []config.FolderDeviceConfiguration{
  34. {
  35. DeviceID: device1,
  36. },
  37. },
  38. }
  39. _defaultConfig := config.Configuration{
  40. Folders: []config.FolderConfiguration{defaultFolderConfig},
  41. Devices: []config.DeviceConfiguration{
  42. {
  43. DeviceID: device1,
  44. },
  45. },
  46. Options: config.OptionsConfiguration{
  47. // Don't remove temporaries directly on startup
  48. KeepTemporariesH: 1,
  49. },
  50. }
  51. defaultConfig = config.Wrap("/tmp/test", _defaultConfig)
  52. }
  53. var testDataExpected = map[string]protocol.FileInfo{
  54. "foo": {
  55. Name: "foo",
  56. Flags: 0,
  57. Modified: 0,
  58. Blocks: []protocol.BlockInfo{{Offset: 0x0, Size: 0x7, Hash: []uint8{0xae, 0xc0, 0x70, 0x64, 0x5f, 0xe5, 0x3e, 0xe3, 0xb3, 0x76, 0x30, 0x59, 0x37, 0x61, 0x34, 0xf0, 0x58, 0xcc, 0x33, 0x72, 0x47, 0xc9, 0x78, 0xad, 0xd1, 0x78, 0xb6, 0xcc, 0xdf, 0xb0, 0x1, 0x9f}}},
  59. },
  60. "empty": {
  61. Name: "empty",
  62. Flags: 0,
  63. Modified: 0,
  64. Blocks: []protocol.BlockInfo{{Offset: 0x0, Size: 0x0, Hash: []uint8{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}}},
  65. },
  66. "bar": {
  67. Name: "bar",
  68. Flags: 0,
  69. Modified: 0,
  70. Blocks: []protocol.BlockInfo{{Offset: 0x0, Size: 0xa, Hash: []uint8{0x2f, 0x72, 0xcc, 0x11, 0xa6, 0xfc, 0xd0, 0x27, 0x1e, 0xce, 0xf8, 0xc6, 0x10, 0x56, 0xee, 0x1e, 0xb1, 0x24, 0x3b, 0xe3, 0x80, 0x5b, 0xf9, 0xa9, 0xdf, 0x98, 0xf9, 0x2f, 0x76, 0x36, 0xb0, 0x5c}}},
  71. },
  72. }
  73. func init() {
  74. // Fix expected test data to match reality
  75. for n, f := range testDataExpected {
  76. fi, _ := os.Stat("testdata/" + n)
  77. f.Flags = uint32(fi.Mode())
  78. f.Modified = fi.ModTime().Unix()
  79. testDataExpected[n] = f
  80. }
  81. }
  82. func TestRequest(t *testing.T) {
  83. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  84. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  85. // device1 shares default, but device2 doesn't
  86. m.AddFolder(defaultFolderConfig)
  87. m.StartFolderRO("default")
  88. m.ScanFolder("default")
  89. // Existing, shared file
  90. bs, err := m.Request(device1, "default", "foo", 0, 6, nil, 0, nil)
  91. if err != nil {
  92. t.Error(err)
  93. }
  94. if bytes.Compare(bs, []byte("foobar")) != 0 {
  95. t.Errorf("Incorrect data from request: %q", string(bs))
  96. }
  97. // Existing, nonshared file
  98. bs, err = m.Request(device2, "default", "foo", 0, 6, nil, 0, nil)
  99. if err == nil {
  100. t.Error("Unexpected nil error on insecure file read")
  101. }
  102. if bs != nil {
  103. t.Errorf("Unexpected non nil data on insecure file read: %q", string(bs))
  104. }
  105. // Nonexistent file
  106. bs, err = m.Request(device1, "default", "nonexistent", 0, 6, nil, 0, nil)
  107. if err == nil {
  108. t.Error("Unexpected nil error on insecure file read")
  109. }
  110. if bs != nil {
  111. t.Errorf("Unexpected non nil data on insecure file read: %q", string(bs))
  112. }
  113. // Shared folder, but disallowed file name
  114. bs, err = m.Request(device1, "default", "../walk.go", 0, 6, nil, 0, nil)
  115. if err == nil {
  116. t.Error("Unexpected nil error on insecure file read")
  117. }
  118. if bs != nil {
  119. t.Errorf("Unexpected non nil data on insecure file read: %q", string(bs))
  120. }
  121. // Larger block than available
  122. bs, err = m.Request(device1, "default", "foo", 0, 42, nil, 0, nil)
  123. if err == nil {
  124. t.Error("Unexpected nil error on insecure file read")
  125. }
  126. if bs != nil {
  127. t.Errorf("Unexpected non nil data on insecure file read: %q", string(bs))
  128. }
  129. // Negative offset
  130. bs, err = m.Request(device1, "default", "foo", -4, 6, nil, 0, nil)
  131. if err == nil {
  132. t.Error("Unexpected nil error on insecure file read")
  133. }
  134. if bs != nil {
  135. t.Errorf("Unexpected non nil data on insecure file read: %q", string(bs))
  136. }
  137. // Negative size
  138. bs, err = m.Request(device1, "default", "foo", 4, -4, nil, 0, nil)
  139. if err == nil {
  140. t.Error("Unexpected nil error on insecure file read")
  141. }
  142. if bs != nil {
  143. t.Errorf("Unexpected non nil data on insecure file read: %q", string(bs))
  144. }
  145. }
  146. func genFiles(n int) []protocol.FileInfo {
  147. files := make([]protocol.FileInfo, n)
  148. t := time.Now().Unix()
  149. for i := 0; i < n; i++ {
  150. files[i] = protocol.FileInfo{
  151. Name: fmt.Sprintf("file%d", i),
  152. Modified: t,
  153. Blocks: []protocol.BlockInfo{{0, 100, []byte("some hash bytes")}},
  154. }
  155. }
  156. return files
  157. }
  158. func BenchmarkIndex10000(b *testing.B) {
  159. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  160. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  161. m.AddFolder(defaultFolderConfig)
  162. m.ScanFolder("default")
  163. files := genFiles(10000)
  164. b.ResetTimer()
  165. for i := 0; i < b.N; i++ {
  166. m.Index(device1, "default", files, 0, nil)
  167. }
  168. }
  169. func BenchmarkIndex00100(b *testing.B) {
  170. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  171. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  172. m.AddFolder(defaultFolderConfig)
  173. m.ScanFolder("default")
  174. files := genFiles(100)
  175. b.ResetTimer()
  176. for i := 0; i < b.N; i++ {
  177. m.Index(device1, "default", files, 0, nil)
  178. }
  179. }
  180. func BenchmarkIndexUpdate10000f10000(b *testing.B) {
  181. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  182. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  183. m.AddFolder(defaultFolderConfig)
  184. m.ScanFolder("default")
  185. files := genFiles(10000)
  186. m.Index(device1, "default", files, 0, nil)
  187. b.ResetTimer()
  188. for i := 0; i < b.N; i++ {
  189. m.IndexUpdate(device1, "default", files, 0, nil)
  190. }
  191. }
  192. func BenchmarkIndexUpdate10000f00100(b *testing.B) {
  193. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  194. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  195. m.AddFolder(defaultFolderConfig)
  196. m.ScanFolder("default")
  197. files := genFiles(10000)
  198. m.Index(device1, "default", files, 0, nil)
  199. ufiles := genFiles(100)
  200. b.ResetTimer()
  201. for i := 0; i < b.N; i++ {
  202. m.IndexUpdate(device1, "default", ufiles, 0, nil)
  203. }
  204. }
  205. func BenchmarkIndexUpdate10000f00001(b *testing.B) {
  206. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  207. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  208. m.AddFolder(defaultFolderConfig)
  209. m.ScanFolder("default")
  210. files := genFiles(10000)
  211. m.Index(device1, "default", files, 0, nil)
  212. ufiles := genFiles(1)
  213. b.ResetTimer()
  214. for i := 0; i < b.N; i++ {
  215. m.IndexUpdate(device1, "default", ufiles, 0, nil)
  216. }
  217. }
  218. type FakeConnection struct {
  219. id protocol.DeviceID
  220. requestData []byte
  221. }
  222. func (FakeConnection) Close() error {
  223. return nil
  224. }
  225. func (f FakeConnection) ID() protocol.DeviceID {
  226. return f.id
  227. }
  228. func (f FakeConnection) Name() string {
  229. return ""
  230. }
  231. func (f FakeConnection) Option(string) string {
  232. return ""
  233. }
  234. func (FakeConnection) Index(string, []protocol.FileInfo, uint32, []protocol.Option) error {
  235. return nil
  236. }
  237. func (FakeConnection) IndexUpdate(string, []protocol.FileInfo, uint32, []protocol.Option) error {
  238. return nil
  239. }
  240. func (f FakeConnection) Request(folder, name string, offset int64, size int, hash []byte, flags uint32, options []protocol.Option) ([]byte, error) {
  241. return f.requestData, nil
  242. }
  243. func (FakeConnection) ClusterConfig(protocol.ClusterConfigMessage) {}
  244. func (FakeConnection) Ping() bool {
  245. return true
  246. }
  247. func (FakeConnection) Statistics() protocol.Statistics {
  248. return protocol.Statistics{}
  249. }
  250. func BenchmarkRequest(b *testing.B) {
  251. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  252. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  253. m.AddFolder(defaultFolderConfig)
  254. m.ScanFolder("default")
  255. const n = 1000
  256. files := make([]protocol.FileInfo, n)
  257. t := time.Now().Unix()
  258. for i := 0; i < n; i++ {
  259. files[i] = protocol.FileInfo{
  260. Name: fmt.Sprintf("file%d", i),
  261. Modified: t,
  262. Blocks: []protocol.BlockInfo{{0, 100, []byte("some hash bytes")}},
  263. }
  264. }
  265. fc := FakeConnection{
  266. id: device1,
  267. requestData: []byte("some data to return"),
  268. }
  269. m.AddConnection(fc, fc)
  270. m.Index(device1, "default", files, 0, nil)
  271. b.ResetTimer()
  272. for i := 0; i < b.N; i++ {
  273. data, err := m.requestGlobal(device1, "default", files[i%n].Name, 0, 32, nil, 0, nil)
  274. if err != nil {
  275. b.Error(err)
  276. }
  277. if data == nil {
  278. b.Error("nil data")
  279. }
  280. }
  281. }
  282. func TestDeviceRename(t *testing.T) {
  283. ccm := protocol.ClusterConfigMessage{
  284. ClientName: "syncthing",
  285. ClientVersion: "v0.9.4",
  286. }
  287. defer os.Remove("tmpconfig.xml")
  288. cfg := config.New(device1)
  289. cfg.Devices = []config.DeviceConfiguration{
  290. {
  291. DeviceID: device1,
  292. },
  293. }
  294. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  295. m := NewModel(config.Wrap("tmpconfig.xml", cfg), protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  296. if cfg.Devices[0].Name != "" {
  297. t.Errorf("Device already has a name")
  298. }
  299. m.ClusterConfig(device1, ccm)
  300. if cfg.Devices[0].Name != "" {
  301. t.Errorf("Device already has a name")
  302. }
  303. ccm.Options = []protocol.Option{
  304. {
  305. Key: "name",
  306. Value: "tester",
  307. },
  308. }
  309. m.ClusterConfig(device1, ccm)
  310. if cfg.Devices[0].Name != "tester" {
  311. t.Errorf("Device did not get a name")
  312. }
  313. ccm.Options[0].Value = "tester2"
  314. m.ClusterConfig(device1, ccm)
  315. if cfg.Devices[0].Name != "tester" {
  316. t.Errorf("Device name got overwritten")
  317. }
  318. cfgw, err := config.Load("tmpconfig.xml", protocol.LocalDeviceID)
  319. if err != nil {
  320. t.Error(err)
  321. return
  322. }
  323. if cfgw.Devices()[device1].Name != "tester" {
  324. t.Errorf("Device name not saved in config")
  325. }
  326. }
  327. func TestClusterConfig(t *testing.T) {
  328. cfg := config.New(device1)
  329. cfg.Devices = []config.DeviceConfiguration{
  330. {
  331. DeviceID: device1,
  332. Introducer: true,
  333. },
  334. {
  335. DeviceID: device2,
  336. },
  337. }
  338. cfg.Folders = []config.FolderConfiguration{
  339. {
  340. ID: "folder1",
  341. Devices: []config.FolderDeviceConfiguration{
  342. {DeviceID: device1},
  343. {DeviceID: device2},
  344. },
  345. },
  346. {
  347. ID: "folder2",
  348. Devices: []config.FolderDeviceConfiguration{
  349. {DeviceID: device1},
  350. {DeviceID: device2},
  351. },
  352. },
  353. }
  354. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  355. m := NewModel(config.Wrap("/tmp/test", cfg), protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  356. m.AddFolder(cfg.Folders[0])
  357. m.AddFolder(cfg.Folders[1])
  358. cm := m.clusterConfig(device2)
  359. if l := len(cm.Folders); l != 2 {
  360. t.Fatalf("Incorrect number of folders %d != 2", l)
  361. }
  362. r := cm.Folders[0]
  363. if r.ID != "folder1" {
  364. t.Errorf("Incorrect folder %q != folder1", r.ID)
  365. }
  366. if l := len(r.Devices); l != 2 {
  367. t.Errorf("Incorrect number of devices %d != 2", l)
  368. }
  369. if id := r.Devices[0].ID; bytes.Compare(id, device1[:]) != 0 {
  370. t.Errorf("Incorrect device ID %x != %x", id, device1)
  371. }
  372. if r.Devices[0].Flags&protocol.FlagIntroducer == 0 {
  373. t.Error("Device1 should be flagged as Introducer")
  374. }
  375. if id := r.Devices[1].ID; bytes.Compare(id, device2[:]) != 0 {
  376. t.Errorf("Incorrect device ID %x != %x", id, device2)
  377. }
  378. if r.Devices[1].Flags&protocol.FlagIntroducer != 0 {
  379. t.Error("Device2 should not be flagged as Introducer")
  380. }
  381. r = cm.Folders[1]
  382. if r.ID != "folder2" {
  383. t.Errorf("Incorrect folder %q != folder2", r.ID)
  384. }
  385. if l := len(r.Devices); l != 2 {
  386. t.Errorf("Incorrect number of devices %d != 2", l)
  387. }
  388. if id := r.Devices[0].ID; bytes.Compare(id, device1[:]) != 0 {
  389. t.Errorf("Incorrect device ID %x != %x", id, device1)
  390. }
  391. if r.Devices[0].Flags&protocol.FlagIntroducer == 0 {
  392. t.Error("Device1 should be flagged as Introducer")
  393. }
  394. if id := r.Devices[1].ID; bytes.Compare(id, device2[:]) != 0 {
  395. t.Errorf("Incorrect device ID %x != %x", id, device2)
  396. }
  397. if r.Devices[1].Flags&protocol.FlagIntroducer != 0 {
  398. t.Error("Device2 should not be flagged as Introducer")
  399. }
  400. }
  401. func TestIgnores(t *testing.T) {
  402. arrEqual := func(a, b []string) bool {
  403. if len(a) != len(b) {
  404. return false
  405. }
  406. for i := range a {
  407. if a[i] != b[i] {
  408. return false
  409. }
  410. }
  411. return true
  412. }
  413. // Assure a clean start state
  414. ioutil.WriteFile("testdata/.stignore", []byte(".*\nquux\n"), 0644)
  415. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  416. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  417. m.AddFolder(defaultFolderConfig)
  418. m.StartFolderRO("default")
  419. expected := []string{
  420. ".*",
  421. "quux",
  422. }
  423. ignores, _, err := m.GetIgnores("default")
  424. if err != nil {
  425. t.Error(err)
  426. }
  427. if !arrEqual(ignores, expected) {
  428. t.Errorf("Incorrect ignores: %v != %v", ignores, expected)
  429. }
  430. ignores = append(ignores, "pox")
  431. err = m.SetIgnores("default", ignores)
  432. if err != nil {
  433. t.Error(err)
  434. }
  435. ignores2, _, err := m.GetIgnores("default")
  436. if err != nil {
  437. t.Error(err)
  438. }
  439. if arrEqual(expected, ignores2) {
  440. t.Errorf("Incorrect ignores: %v == %v", ignores2, expected)
  441. }
  442. if !arrEqual(ignores, ignores2) {
  443. t.Errorf("Incorrect ignores: %v != %v", ignores2, ignores)
  444. }
  445. err = m.SetIgnores("default", expected)
  446. if err != nil {
  447. t.Error(err)
  448. }
  449. ignores, _, err = m.GetIgnores("default")
  450. if err != nil {
  451. t.Error(err)
  452. }
  453. if !arrEqual(ignores, expected) {
  454. t.Errorf("Incorrect ignores: %v != %v", ignores, expected)
  455. }
  456. ignores, _, err = m.GetIgnores("doesnotexist")
  457. if err == nil {
  458. t.Error("No error")
  459. }
  460. err = m.SetIgnores("doesnotexist", expected)
  461. if err == nil {
  462. t.Error("No error")
  463. }
  464. m.AddFolder(config.FolderConfiguration{ID: "fresh", RawPath: "XXX"})
  465. ignores, _, err = m.GetIgnores("fresh")
  466. if err != nil {
  467. t.Error(err)
  468. }
  469. if len(ignores) > 0 {
  470. t.Errorf("Expected no ignores, got: %v", ignores)
  471. }
  472. }
  473. func TestRefuseUnknownBits(t *testing.T) {
  474. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  475. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  476. m.AddFolder(defaultFolderConfig)
  477. m.ScanFolder("default")
  478. m.Index(device1, "default", []protocol.FileInfo{
  479. {
  480. Name: "invalid1",
  481. Flags: (protocol.FlagsAll + 1) &^ protocol.FlagInvalid,
  482. },
  483. {
  484. Name: "invalid2",
  485. Flags: (protocol.FlagsAll + 2) &^ protocol.FlagInvalid,
  486. },
  487. {
  488. Name: "invalid3",
  489. Flags: (1 << 31) &^ protocol.FlagInvalid,
  490. },
  491. {
  492. Name: "valid",
  493. Flags: protocol.FlagsAll &^ (protocol.FlagInvalid | protocol.FlagSymlink),
  494. },
  495. }, 0, nil)
  496. for _, name := range []string{"invalid1", "invalid2", "invalid3"} {
  497. f, ok := m.CurrentGlobalFile("default", name)
  498. if ok || f.Name == name {
  499. t.Error("Invalid file found or name match")
  500. }
  501. }
  502. f, ok := m.CurrentGlobalFile("default", "valid")
  503. if !ok || f.Name != "valid" {
  504. t.Error("Valid file not found or name mismatch", ok, f)
  505. }
  506. }
  507. func TestROScanRecovery(t *testing.T) {
  508. ldb, _ := leveldb.Open(storage.NewMemStorage(), nil)
  509. set := db.NewFileSet("default", ldb)
  510. set.Update(protocol.LocalDeviceID, []protocol.FileInfo{
  511. {Name: "dummyfile"},
  512. })
  513. fcfg := config.FolderConfiguration{
  514. ID: "default",
  515. RawPath: "testdata/rotestfolder",
  516. RescanIntervalS: 1,
  517. }
  518. cfg := config.Wrap("/tmp/test", config.Configuration{
  519. Folders: []config.FolderConfiguration{fcfg},
  520. Devices: []config.DeviceConfiguration{
  521. {
  522. DeviceID: device1,
  523. },
  524. },
  525. })
  526. os.RemoveAll(fcfg.RawPath)
  527. m := NewModel(cfg, protocol.LocalDeviceID, "device", "syncthing", "dev", ldb)
  528. m.AddFolder(fcfg)
  529. m.StartFolderRO("default")
  530. waitFor := func(status string) error {
  531. timeout := time.Now().Add(2 * time.Second)
  532. for {
  533. if time.Now().After(timeout) {
  534. return fmt.Errorf("Timed out waiting for status: %s, current status: %s", status, m.cfg.Folders()["default"].Invalid)
  535. }
  536. _, _, err := m.State("default")
  537. if err == nil && status == "" {
  538. return nil
  539. }
  540. if err != nil && err.Error() == status {
  541. return nil
  542. }
  543. time.Sleep(10 * time.Millisecond)
  544. }
  545. }
  546. if err := waitFor("folder path missing"); err != nil {
  547. t.Error(err)
  548. return
  549. }
  550. os.Mkdir(fcfg.RawPath, 0700)
  551. if err := waitFor("folder marker missing"); err != nil {
  552. t.Error(err)
  553. return
  554. }
  555. fd, err := os.Create(filepath.Join(fcfg.RawPath, ".stfolder"))
  556. if err != nil {
  557. t.Error(err)
  558. return
  559. }
  560. fd.Close()
  561. if err := waitFor(""); err != nil {
  562. t.Error(err)
  563. return
  564. }
  565. os.Remove(filepath.Join(fcfg.RawPath, ".stfolder"))
  566. if err := waitFor("folder marker missing"); err != nil {
  567. t.Error(err)
  568. return
  569. }
  570. os.Remove(fcfg.RawPath)
  571. if err := waitFor("folder path missing"); err != nil {
  572. t.Error(err)
  573. return
  574. }
  575. }
  576. func TestRWScanRecovery(t *testing.T) {
  577. ldb, _ := leveldb.Open(storage.NewMemStorage(), nil)
  578. set := db.NewFileSet("default", ldb)
  579. set.Update(protocol.LocalDeviceID, []protocol.FileInfo{
  580. {Name: "dummyfile"},
  581. })
  582. fcfg := config.FolderConfiguration{
  583. ID: "default",
  584. RawPath: "testdata/rwtestfolder",
  585. RescanIntervalS: 1,
  586. }
  587. cfg := config.Wrap("/tmp/test", config.Configuration{
  588. Folders: []config.FolderConfiguration{fcfg},
  589. Devices: []config.DeviceConfiguration{
  590. {
  591. DeviceID: device1,
  592. },
  593. },
  594. })
  595. os.RemoveAll(fcfg.RawPath)
  596. m := NewModel(cfg, protocol.LocalDeviceID, "device", "syncthing", "dev", ldb)
  597. m.AddFolder(fcfg)
  598. m.StartFolderRW("default")
  599. waitFor := func(status string) error {
  600. timeout := time.Now().Add(2 * time.Second)
  601. for {
  602. if time.Now().After(timeout) {
  603. return fmt.Errorf("Timed out waiting for status: %s, current status: %s", status, m.cfg.Folders()["default"].Invalid)
  604. }
  605. _, _, err := m.State("default")
  606. if err == nil && status == "" {
  607. return nil
  608. }
  609. if err != nil && err.Error() == status {
  610. return nil
  611. }
  612. time.Sleep(10 * time.Millisecond)
  613. }
  614. }
  615. if err := waitFor("folder path missing"); err != nil {
  616. t.Error(err)
  617. return
  618. }
  619. os.Mkdir(fcfg.RawPath, 0700)
  620. if err := waitFor("folder marker missing"); err != nil {
  621. t.Error(err)
  622. return
  623. }
  624. fd, err := os.Create(filepath.Join(fcfg.RawPath, ".stfolder"))
  625. if err != nil {
  626. t.Error(err)
  627. return
  628. }
  629. fd.Close()
  630. if err := waitFor(""); err != nil {
  631. t.Error(err)
  632. return
  633. }
  634. os.Remove(filepath.Join(fcfg.RawPath, ".stfolder"))
  635. if err := waitFor("folder marker missing"); err != nil {
  636. t.Error(err)
  637. return
  638. }
  639. os.Remove(fcfg.RawPath)
  640. if err := waitFor("folder path missing"); err != nil {
  641. t.Error(err)
  642. return
  643. }
  644. }
  645. func TestGlobalDirectoryTree(t *testing.T) {
  646. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  647. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  648. m.AddFolder(defaultFolderConfig)
  649. b := func(isfile bool, path ...string) protocol.FileInfo {
  650. flags := uint32(protocol.FlagDirectory)
  651. blocks := []protocol.BlockInfo{}
  652. if isfile {
  653. flags = 0
  654. blocks = []protocol.BlockInfo{{Offset: 0x0, Size: 0xa, Hash: []uint8{0x2f, 0x72, 0xcc, 0x11, 0xa6, 0xfc, 0xd0, 0x27, 0x1e, 0xce, 0xf8, 0xc6, 0x10, 0x56, 0xee, 0x1e, 0xb1, 0x24, 0x3b, 0xe3, 0x80, 0x5b, 0xf9, 0xa9, 0xdf, 0x98, 0xf9, 0x2f, 0x76, 0x36, 0xb0, 0x5c}}}
  655. }
  656. return protocol.FileInfo{
  657. Name: filepath.Join(path...),
  658. Flags: flags,
  659. Modified: 0x666,
  660. Blocks: blocks,
  661. }
  662. }
  663. filedata := []interface{}{time.Unix(0x666, 0), 0xa}
  664. testdata := []protocol.FileInfo{
  665. b(false, "another"),
  666. b(false, "another", "directory"),
  667. b(true, "another", "directory", "afile"),
  668. b(false, "another", "directory", "with"),
  669. b(false, "another", "directory", "with", "a"),
  670. b(true, "another", "directory", "with", "a", "file"),
  671. b(true, "another", "directory", "with", "file"),
  672. b(true, "another", "file"),
  673. b(false, "other"),
  674. b(false, "other", "rand"),
  675. b(false, "other", "random"),
  676. b(false, "other", "random", "dir"),
  677. b(false, "other", "random", "dirx"),
  678. b(false, "other", "randomx"),
  679. b(false, "some"),
  680. b(false, "some", "directory"),
  681. b(false, "some", "directory", "with"),
  682. b(false, "some", "directory", "with", "a"),
  683. b(true, "some", "directory", "with", "a", "file"),
  684. b(true, "rootfile"),
  685. }
  686. expectedResult := map[string]interface{}{
  687. "another": map[string]interface{}{
  688. "directory": map[string]interface{}{
  689. "afile": filedata,
  690. "with": map[string]interface{}{
  691. "a": map[string]interface{}{
  692. "file": filedata,
  693. },
  694. "file": filedata,
  695. },
  696. },
  697. "file": filedata,
  698. },
  699. "other": map[string]interface{}{
  700. "rand": map[string]interface{}{},
  701. "random": map[string]interface{}{
  702. "dir": map[string]interface{}{},
  703. "dirx": map[string]interface{}{},
  704. },
  705. "randomx": map[string]interface{}{},
  706. },
  707. "some": map[string]interface{}{
  708. "directory": map[string]interface{}{
  709. "with": map[string]interface{}{
  710. "a": map[string]interface{}{
  711. "file": filedata,
  712. },
  713. },
  714. },
  715. },
  716. "rootfile": filedata,
  717. }
  718. mm := func(data interface{}) string {
  719. bytes, err := json.Marshal(data)
  720. if err != nil {
  721. panic(err)
  722. }
  723. return string(bytes)
  724. }
  725. m.Index(device1, "default", testdata, 0, nil)
  726. result := m.GlobalDirectoryTree("default", "", -1, false)
  727. if mm(result) != mm(expectedResult) {
  728. t.Errorf("Does not match:\n%#v\n%#v", result, expectedResult)
  729. }
  730. result = m.GlobalDirectoryTree("default", "another", -1, false)
  731. if mm(result) != mm(expectedResult["another"]) {
  732. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(expectedResult["another"]))
  733. }
  734. result = m.GlobalDirectoryTree("default", "", 0, false)
  735. currentResult := map[string]interface{}{
  736. "another": map[string]interface{}{},
  737. "other": map[string]interface{}{},
  738. "some": map[string]interface{}{},
  739. "rootfile": filedata,
  740. }
  741. if mm(result) != mm(currentResult) {
  742. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  743. }
  744. result = m.GlobalDirectoryTree("default", "", 1, false)
  745. currentResult = map[string]interface{}{
  746. "another": map[string]interface{}{
  747. "directory": map[string]interface{}{},
  748. "file": filedata,
  749. },
  750. "other": map[string]interface{}{
  751. "rand": map[string]interface{}{},
  752. "random": map[string]interface{}{},
  753. "randomx": map[string]interface{}{},
  754. },
  755. "some": map[string]interface{}{
  756. "directory": map[string]interface{}{},
  757. },
  758. "rootfile": filedata,
  759. }
  760. if mm(result) != mm(currentResult) {
  761. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  762. }
  763. result = m.GlobalDirectoryTree("default", "", -1, true)
  764. currentResult = map[string]interface{}{
  765. "another": map[string]interface{}{
  766. "directory": map[string]interface{}{
  767. "with": map[string]interface{}{
  768. "a": map[string]interface{}{},
  769. },
  770. },
  771. },
  772. "other": map[string]interface{}{
  773. "rand": map[string]interface{}{},
  774. "random": map[string]interface{}{
  775. "dir": map[string]interface{}{},
  776. "dirx": map[string]interface{}{},
  777. },
  778. "randomx": map[string]interface{}{},
  779. },
  780. "some": map[string]interface{}{
  781. "directory": map[string]interface{}{
  782. "with": map[string]interface{}{
  783. "a": map[string]interface{}{},
  784. },
  785. },
  786. },
  787. }
  788. if mm(result) != mm(currentResult) {
  789. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  790. }
  791. result = m.GlobalDirectoryTree("default", "", 1, true)
  792. currentResult = map[string]interface{}{
  793. "another": map[string]interface{}{
  794. "directory": map[string]interface{}{},
  795. },
  796. "other": map[string]interface{}{
  797. "rand": map[string]interface{}{},
  798. "random": map[string]interface{}{},
  799. "randomx": map[string]interface{}{},
  800. },
  801. "some": map[string]interface{}{
  802. "directory": map[string]interface{}{},
  803. },
  804. }
  805. if mm(result) != mm(currentResult) {
  806. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  807. }
  808. result = m.GlobalDirectoryTree("default", "another", 0, false)
  809. currentResult = map[string]interface{}{
  810. "directory": map[string]interface{}{},
  811. "file": filedata,
  812. }
  813. if mm(result) != mm(currentResult) {
  814. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  815. }
  816. result = m.GlobalDirectoryTree("default", "some/directory", 0, false)
  817. currentResult = map[string]interface{}{
  818. "with": map[string]interface{}{},
  819. }
  820. if mm(result) != mm(currentResult) {
  821. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  822. }
  823. result = m.GlobalDirectoryTree("default", "some/directory", 1, false)
  824. currentResult = map[string]interface{}{
  825. "with": map[string]interface{}{
  826. "a": map[string]interface{}{},
  827. },
  828. }
  829. if mm(result) != mm(currentResult) {
  830. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  831. }
  832. result = m.GlobalDirectoryTree("default", "some/directory", 2, false)
  833. currentResult = map[string]interface{}{
  834. "with": map[string]interface{}{
  835. "a": map[string]interface{}{
  836. "file": filedata,
  837. },
  838. },
  839. }
  840. if mm(result) != mm(currentResult) {
  841. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  842. }
  843. result = m.GlobalDirectoryTree("default", "another", -1, true)
  844. currentResult = map[string]interface{}{
  845. "directory": map[string]interface{}{
  846. "with": map[string]interface{}{
  847. "a": map[string]interface{}{},
  848. },
  849. },
  850. }
  851. if mm(result) != mm(currentResult) {
  852. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  853. }
  854. // No prefix matching!
  855. result = m.GlobalDirectoryTree("default", "som", -1, false)
  856. currentResult = map[string]interface{}{}
  857. if mm(result) != mm(currentResult) {
  858. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  859. }
  860. }
  861. func TestGlobalDirectorySelfFixing(t *testing.T) {
  862. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  863. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  864. m.AddFolder(defaultFolderConfig)
  865. b := func(isfile bool, path ...string) protocol.FileInfo {
  866. flags := uint32(protocol.FlagDirectory)
  867. blocks := []protocol.BlockInfo{}
  868. if isfile {
  869. flags = 0
  870. blocks = []protocol.BlockInfo{{Offset: 0x0, Size: 0xa, Hash: []uint8{0x2f, 0x72, 0xcc, 0x11, 0xa6, 0xfc, 0xd0, 0x27, 0x1e, 0xce, 0xf8, 0xc6, 0x10, 0x56, 0xee, 0x1e, 0xb1, 0x24, 0x3b, 0xe3, 0x80, 0x5b, 0xf9, 0xa9, 0xdf, 0x98, 0xf9, 0x2f, 0x76, 0x36, 0xb0, 0x5c}}}
  871. }
  872. return protocol.FileInfo{
  873. Name: filepath.Join(path...),
  874. Flags: flags,
  875. Modified: 0x666,
  876. Blocks: blocks,
  877. }
  878. }
  879. filedata := []interface{}{time.Unix(0x666, 0).Format(time.RFC3339), 0xa}
  880. testdata := []protocol.FileInfo{
  881. b(true, "another", "directory", "afile"),
  882. b(true, "another", "directory", "with", "a", "file"),
  883. b(true, "another", "directory", "with", "file"),
  884. b(false, "other", "random", "dirx"),
  885. b(false, "other", "randomx"),
  886. b(false, "some", "directory", "with", "x"),
  887. b(true, "some", "directory", "with", "a", "file"),
  888. b(false, "this", "is", "a", "deep", "invalid", "directory"),
  889. b(true, "xthis", "is", "a", "deep", "invalid", "file"),
  890. }
  891. expectedResult := map[string]interface{}{
  892. "another": map[string]interface{}{
  893. "directory": map[string]interface{}{
  894. "afile": filedata,
  895. "with": map[string]interface{}{
  896. "a": map[string]interface{}{
  897. "file": filedata,
  898. },
  899. "file": filedata,
  900. },
  901. },
  902. },
  903. "other": map[string]interface{}{
  904. "random": map[string]interface{}{
  905. "dirx": map[string]interface{}{},
  906. },
  907. "randomx": map[string]interface{}{},
  908. },
  909. "some": map[string]interface{}{
  910. "directory": map[string]interface{}{
  911. "with": map[string]interface{}{
  912. "a": map[string]interface{}{
  913. "file": filedata,
  914. },
  915. "x": map[string]interface{}{},
  916. },
  917. },
  918. },
  919. "this": map[string]interface{}{
  920. "is": map[string]interface{}{
  921. "a": map[string]interface{}{
  922. "deep": map[string]interface{}{
  923. "invalid": map[string]interface{}{
  924. "directory": map[string]interface{}{},
  925. },
  926. },
  927. },
  928. },
  929. },
  930. "xthis": map[string]interface{}{
  931. "is": map[string]interface{}{
  932. "a": map[string]interface{}{
  933. "deep": map[string]interface{}{
  934. "invalid": map[string]interface{}{
  935. "file": filedata,
  936. },
  937. },
  938. },
  939. },
  940. },
  941. }
  942. mm := func(data interface{}) string {
  943. bytes, err := json.Marshal(data)
  944. if err != nil {
  945. panic(err)
  946. }
  947. return string(bytes)
  948. }
  949. m.Index(device1, "default", testdata, 0, nil)
  950. result := m.GlobalDirectoryTree("default", "", -1, false)
  951. if mm(result) != mm(expectedResult) {
  952. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(expectedResult))
  953. }
  954. result = m.GlobalDirectoryTree("default", "xthis/is/a/deep", -1, false)
  955. currentResult := map[string]interface{}{
  956. "invalid": map[string]interface{}{
  957. "file": filedata,
  958. },
  959. }
  960. if mm(result) != mm(currentResult) {
  961. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  962. }
  963. result = m.GlobalDirectoryTree("default", "xthis/is/a/deep", -1, true)
  964. currentResult = map[string]interface{}{
  965. "invalid": map[string]interface{}{},
  966. }
  967. if mm(result) != mm(currentResult) {
  968. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  969. }
  970. // !!! This is actually BAD, because we don't have enough level allowance
  971. // to accept this file, hence the tree is left unbuilt !!!
  972. result = m.GlobalDirectoryTree("default", "xthis", 1, false)
  973. currentResult = map[string]interface{}{}
  974. if mm(result) != mm(currentResult) {
  975. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  976. }
  977. }
  978. func genDeepFiles(n, d int) []protocol.FileInfo {
  979. rand.Seed(int64(n))
  980. files := make([]protocol.FileInfo, n)
  981. t := time.Now().Unix()
  982. for i := 0; i < n; i++ {
  983. path := ""
  984. for i := 0; i <= d; i++ {
  985. path = filepath.Join(path, strconv.Itoa(rand.Int()))
  986. }
  987. sofar := ""
  988. for _, path := range filepath.SplitList(path) {
  989. sofar = filepath.Join(sofar, path)
  990. files[i] = protocol.FileInfo{
  991. Name: sofar,
  992. }
  993. i++
  994. }
  995. files[i].Modified = t
  996. files[i].Blocks = []protocol.BlockInfo{{0, 100, []byte("some hash bytes")}}
  997. }
  998. return files
  999. }
  1000. func BenchmarkTree_10000_50(b *testing.B) {
  1001. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  1002. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  1003. m.AddFolder(defaultFolderConfig)
  1004. m.ScanFolder("default")
  1005. files := genDeepFiles(10000, 50)
  1006. m.Index(device1, "default", files, 0, nil)
  1007. b.ResetTimer()
  1008. for i := 0; i < b.N; i++ {
  1009. m.GlobalDirectoryTree("default", "", -1, false)
  1010. }
  1011. }
  1012. func BenchmarkTree_10000_10(b *testing.B) {
  1013. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  1014. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  1015. m.AddFolder(defaultFolderConfig)
  1016. m.ScanFolder("default")
  1017. files := genDeepFiles(10000, 10)
  1018. m.Index(device1, "default", files, 0, nil)
  1019. b.ResetTimer()
  1020. for i := 0; i < b.N; i++ {
  1021. m.GlobalDirectoryTree("default", "", -1, false)
  1022. }
  1023. }
  1024. func BenchmarkTree_00100_50(b *testing.B) {
  1025. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  1026. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  1027. m.AddFolder(defaultFolderConfig)
  1028. m.ScanFolder("default")
  1029. files := genDeepFiles(100, 50)
  1030. m.Index(device1, "default", files, 0, nil)
  1031. b.ResetTimer()
  1032. for i := 0; i < b.N; i++ {
  1033. m.GlobalDirectoryTree("default", "", -1, false)
  1034. }
  1035. }
  1036. func BenchmarkTree_00100_10(b *testing.B) {
  1037. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  1038. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
  1039. m.AddFolder(defaultFolderConfig)
  1040. m.ScanFolder("default")
  1041. files := genDeepFiles(100, 10)
  1042. m.Index(device1, "default", files, 0, nil)
  1043. b.ResetTimer()
  1044. for i := 0; i < b.N; i++ {
  1045. m.GlobalDirectoryTree("default", "", -1, false)
  1046. }
  1047. }