model_test.go 27 KB

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