model_test.go 27 KB

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