model_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. "fmt"
  19. "os"
  20. "testing"
  21. "time"
  22. "github.com/syncthing/protocol"
  23. "github.com/syncthing/syncthing/internal/config"
  24. "github.com/syndtr/goleveldb/leveldb"
  25. "github.com/syndtr/goleveldb/leveldb/storage"
  26. )
  27. var device1, device2 protocol.DeviceID
  28. func init() {
  29. device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  30. device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
  31. }
  32. var testDataExpected = map[string]protocol.FileInfo{
  33. "foo": {
  34. Name: "foo",
  35. Flags: 0,
  36. Modified: 0,
  37. 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}}},
  38. },
  39. "empty": {
  40. Name: "empty",
  41. Flags: 0,
  42. Modified: 0,
  43. 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}}},
  44. },
  45. "bar": {
  46. Name: "bar",
  47. Flags: 0,
  48. Modified: 0,
  49. 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}}},
  50. },
  51. }
  52. func init() {
  53. // Fix expected test data to match reality
  54. for n, f := range testDataExpected {
  55. fi, _ := os.Stat("testdata/" + n)
  56. f.Flags = uint32(fi.Mode())
  57. f.Modified = fi.ModTime().Unix()
  58. testDataExpected[n] = f
  59. }
  60. }
  61. func TestRequest(t *testing.T) {
  62. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  63. m := NewModel(config.Wrap("/tmp/test", config.Configuration{}), "device", "syncthing", "dev", db)
  64. // device1 shares default, but device2 doesn't
  65. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata", Devices: []config.FolderDeviceConfiguration{{DeviceID: device1}}})
  66. m.ScanFolder("default")
  67. // Existing, shared file
  68. bs, err := m.Request(device1, "default", "foo", 0, 6)
  69. if err != nil {
  70. t.Error(err)
  71. }
  72. if bytes.Compare(bs, []byte("foobar")) != 0 {
  73. t.Errorf("Incorrect data from request: %q", string(bs))
  74. }
  75. // Existing, nonshared file
  76. bs, err = m.Request(device2, "default", "foo", 0, 6)
  77. if err == nil {
  78. t.Error("Unexpected nil error on insecure file read")
  79. }
  80. if bs != nil {
  81. t.Errorf("Unexpected non nil data on insecure file read: %q", string(bs))
  82. }
  83. // Nonexistent file
  84. bs, err = m.Request(device1, "default", "nonexistent", 0, 6)
  85. if err == nil {
  86. t.Error("Unexpected nil error on insecure file read")
  87. }
  88. if bs != nil {
  89. t.Errorf("Unexpected non nil data on insecure file read: %q", string(bs))
  90. }
  91. // Shared folder, but disallowed file name
  92. bs, err = m.Request(device1, "default", "../walk.go", 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. }
  100. func genFiles(n int) []protocol.FileInfo {
  101. files := make([]protocol.FileInfo, n)
  102. t := time.Now().Unix()
  103. for i := 0; i < n; i++ {
  104. files[i] = protocol.FileInfo{
  105. Name: fmt.Sprintf("file%d", i),
  106. Modified: t,
  107. Blocks: []protocol.BlockInfo{{0, 100, []byte("some hash bytes")}},
  108. }
  109. }
  110. return files
  111. }
  112. func BenchmarkIndex10000(b *testing.B) {
  113. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  114. m := NewModel(nil, "device", "syncthing", "dev", db)
  115. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata"})
  116. m.ScanFolder("default")
  117. files := genFiles(10000)
  118. b.ResetTimer()
  119. for i := 0; i < b.N; i++ {
  120. m.Index(device1, "default", files)
  121. }
  122. }
  123. func BenchmarkIndex00100(b *testing.B) {
  124. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  125. m := NewModel(nil, "device", "syncthing", "dev", db)
  126. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata"})
  127. m.ScanFolder("default")
  128. files := genFiles(100)
  129. b.ResetTimer()
  130. for i := 0; i < b.N; i++ {
  131. m.Index(device1, "default", files)
  132. }
  133. }
  134. func BenchmarkIndexUpdate10000f10000(b *testing.B) {
  135. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  136. m := NewModel(nil, "device", "syncthing", "dev", db)
  137. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata"})
  138. m.ScanFolder("default")
  139. files := genFiles(10000)
  140. m.Index(device1, "default", files)
  141. b.ResetTimer()
  142. for i := 0; i < b.N; i++ {
  143. m.IndexUpdate(device1, "default", files)
  144. }
  145. }
  146. func BenchmarkIndexUpdate10000f00100(b *testing.B) {
  147. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  148. m := NewModel(nil, "device", "syncthing", "dev", db)
  149. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata"})
  150. m.ScanFolder("default")
  151. files := genFiles(10000)
  152. m.Index(device1, "default", files)
  153. ufiles := genFiles(100)
  154. b.ResetTimer()
  155. for i := 0; i < b.N; i++ {
  156. m.IndexUpdate(device1, "default", ufiles)
  157. }
  158. }
  159. func BenchmarkIndexUpdate10000f00001(b *testing.B) {
  160. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  161. m := NewModel(nil, "device", "syncthing", "dev", db)
  162. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata"})
  163. m.ScanFolder("default")
  164. files := genFiles(10000)
  165. m.Index(device1, "default", files)
  166. ufiles := genFiles(1)
  167. b.ResetTimer()
  168. for i := 0; i < b.N; i++ {
  169. m.IndexUpdate(device1, "default", ufiles)
  170. }
  171. }
  172. type FakeConnection struct {
  173. id protocol.DeviceID
  174. requestData []byte
  175. }
  176. func (FakeConnection) Close() error {
  177. return nil
  178. }
  179. func (f FakeConnection) ID() protocol.DeviceID {
  180. return f.id
  181. }
  182. func (f FakeConnection) Name() string {
  183. return ""
  184. }
  185. func (f FakeConnection) Option(string) string {
  186. return ""
  187. }
  188. func (FakeConnection) Index(string, []protocol.FileInfo) error {
  189. return nil
  190. }
  191. func (FakeConnection) IndexUpdate(string, []protocol.FileInfo) error {
  192. return nil
  193. }
  194. func (f FakeConnection) Request(folder, name string, offset int64, size int) ([]byte, error) {
  195. return f.requestData, nil
  196. }
  197. func (FakeConnection) ClusterConfig(protocol.ClusterConfigMessage) {}
  198. func (FakeConnection) Ping() bool {
  199. return true
  200. }
  201. func (FakeConnection) Statistics() protocol.Statistics {
  202. return protocol.Statistics{}
  203. }
  204. func BenchmarkRequest(b *testing.B) {
  205. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  206. m := NewModel(nil, "device", "syncthing", "dev", db)
  207. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata"})
  208. m.ScanFolder("default")
  209. const n = 1000
  210. files := make([]protocol.FileInfo, n)
  211. t := time.Now().Unix()
  212. for i := 0; i < n; i++ {
  213. files[i] = protocol.FileInfo{
  214. Name: fmt.Sprintf("file%d", i),
  215. Modified: t,
  216. Blocks: []protocol.BlockInfo{{0, 100, []byte("some hash bytes")}},
  217. }
  218. }
  219. fc := FakeConnection{
  220. id: device1,
  221. requestData: []byte("some data to return"),
  222. }
  223. m.AddConnection(fc, fc)
  224. m.Index(device1, "default", files)
  225. b.ResetTimer()
  226. for i := 0; i < b.N; i++ {
  227. data, err := m.requestGlobal(device1, "default", files[i%n].Name, 0, 32, nil)
  228. if err != nil {
  229. b.Error(err)
  230. }
  231. if data == nil {
  232. b.Error("nil data")
  233. }
  234. }
  235. }
  236. func TestDeviceRename(t *testing.T) {
  237. ccm := protocol.ClusterConfigMessage{
  238. ClientName: "syncthing",
  239. ClientVersion: "v0.9.4",
  240. }
  241. defer os.Remove("tmpconfig.xml")
  242. cfg := config.New(device1)
  243. cfg.Devices = []config.DeviceConfiguration{
  244. {
  245. DeviceID: device1,
  246. },
  247. }
  248. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  249. m := NewModel(config.Wrap("tmpconfig.xml", cfg), "device", "syncthing", "dev", db)
  250. if cfg.Devices[0].Name != "" {
  251. t.Errorf("Device already has a name")
  252. }
  253. m.ClusterConfig(device1, ccm)
  254. if cfg.Devices[0].Name != "" {
  255. t.Errorf("Device already has a name")
  256. }
  257. ccm.Options = []protocol.Option{
  258. {
  259. Key: "name",
  260. Value: "tester",
  261. },
  262. }
  263. m.ClusterConfig(device1, ccm)
  264. if cfg.Devices[0].Name != "tester" {
  265. t.Errorf("Device did not get a name")
  266. }
  267. ccm.Options[0].Value = "tester2"
  268. m.ClusterConfig(device1, ccm)
  269. if cfg.Devices[0].Name != "tester" {
  270. t.Errorf("Device name got overwritten")
  271. }
  272. cfgw, err := config.Load("tmpconfig.xml", protocol.LocalDeviceID)
  273. if err != nil {
  274. t.Error(err)
  275. return
  276. }
  277. if cfgw.Devices()[device1].Name != "tester" {
  278. t.Errorf("Device name not saved in config")
  279. }
  280. }
  281. func TestClusterConfig(t *testing.T) {
  282. cfg := config.New(device1)
  283. cfg.Devices = []config.DeviceConfiguration{
  284. {
  285. DeviceID: device1,
  286. Introducer: true,
  287. },
  288. {
  289. DeviceID: device2,
  290. },
  291. }
  292. cfg.Folders = []config.FolderConfiguration{
  293. {
  294. ID: "folder1",
  295. Devices: []config.FolderDeviceConfiguration{
  296. {DeviceID: device1},
  297. {DeviceID: device2},
  298. },
  299. },
  300. {
  301. ID: "folder2",
  302. Devices: []config.FolderDeviceConfiguration{
  303. {DeviceID: device1},
  304. {DeviceID: device2},
  305. },
  306. },
  307. }
  308. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  309. m := NewModel(config.Wrap("/tmp/test", cfg), "device", "syncthing", "dev", db)
  310. m.AddFolder(cfg.Folders[0])
  311. m.AddFolder(cfg.Folders[1])
  312. cm := m.clusterConfig(device2)
  313. if l := len(cm.Folders); l != 2 {
  314. t.Fatalf("Incorrect number of folders %d != 2", l)
  315. }
  316. r := cm.Folders[0]
  317. if r.ID != "folder1" {
  318. t.Errorf("Incorrect folder %q != folder1", r.ID)
  319. }
  320. if l := len(r.Devices); l != 2 {
  321. t.Errorf("Incorrect number of devices %d != 2", l)
  322. }
  323. if id := r.Devices[0].ID; bytes.Compare(id, device1[:]) != 0 {
  324. t.Errorf("Incorrect device ID %x != %x", id, device1)
  325. }
  326. if r.Devices[0].Flags&protocol.FlagIntroducer == 0 {
  327. t.Error("Device1 should be flagged as Introducer")
  328. }
  329. if id := r.Devices[1].ID; bytes.Compare(id, device2[:]) != 0 {
  330. t.Errorf("Incorrect device ID %x != %x", id, device2)
  331. }
  332. if r.Devices[1].Flags&protocol.FlagIntroducer != 0 {
  333. t.Error("Device2 should not be flagged as Introducer")
  334. }
  335. r = cm.Folders[1]
  336. if r.ID != "folder2" {
  337. t.Errorf("Incorrect folder %q != folder2", r.ID)
  338. }
  339. if l := len(r.Devices); l != 2 {
  340. t.Errorf("Incorrect number of devices %d != 2", l)
  341. }
  342. if id := r.Devices[0].ID; bytes.Compare(id, device1[:]) != 0 {
  343. t.Errorf("Incorrect device ID %x != %x", id, device1)
  344. }
  345. if r.Devices[0].Flags&protocol.FlagIntroducer == 0 {
  346. t.Error("Device1 should be flagged as Introducer")
  347. }
  348. if id := r.Devices[1].ID; bytes.Compare(id, device2[:]) != 0 {
  349. t.Errorf("Incorrect device ID %x != %x", id, device2)
  350. }
  351. if r.Devices[1].Flags&protocol.FlagIntroducer != 0 {
  352. t.Error("Device2 should not be flagged as Introducer")
  353. }
  354. }
  355. func TestIgnores(t *testing.T) {
  356. arrEqual := func(a, b []string) bool {
  357. if len(a) != len(b) {
  358. return false
  359. }
  360. for i := range a {
  361. if a[i] != b[i] {
  362. return false
  363. }
  364. }
  365. return true
  366. }
  367. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  368. fcfg := config.FolderConfiguration{ID: "default", Path: "testdata"}
  369. cfg := config.Wrap("/tmp", config.Configuration{
  370. Folders: []config.FolderConfiguration{fcfg},
  371. })
  372. m := NewModel(cfg, "device", "syncthing", "dev", db)
  373. m.AddFolder(fcfg)
  374. expected := []string{
  375. ".*",
  376. "quux",
  377. }
  378. ignores, _, err := m.GetIgnores("default")
  379. if err != nil {
  380. t.Error(err)
  381. }
  382. if !arrEqual(ignores, expected) {
  383. t.Errorf("Incorrect ignores: %v != %v", ignores, expected)
  384. }
  385. ignores = append(ignores, "pox")
  386. err = m.SetIgnores("default", ignores)
  387. if err != nil {
  388. t.Error(err)
  389. }
  390. ignores2, _, err := m.GetIgnores("default")
  391. if err != nil {
  392. t.Error(err)
  393. }
  394. if arrEqual(expected, ignores2) {
  395. t.Errorf("Incorrect ignores: %v == %v", ignores2, expected)
  396. }
  397. if !arrEqual(ignores, ignores2) {
  398. t.Errorf("Incorrect ignores: %v != %v", ignores2, ignores)
  399. }
  400. err = m.SetIgnores("default", expected)
  401. if err != nil {
  402. t.Error(err)
  403. }
  404. ignores, _, err = m.GetIgnores("default")
  405. if err != nil {
  406. t.Error(err)
  407. }
  408. if !arrEqual(ignores, expected) {
  409. t.Errorf("Incorrect ignores: %v != %v", ignores, expected)
  410. }
  411. ignores, _, err = m.GetIgnores("doesnotexist")
  412. if err == nil {
  413. t.Error("No error")
  414. }
  415. err = m.SetIgnores("doesnotexist", expected)
  416. if err == nil {
  417. t.Error("No error")
  418. }
  419. m.AddFolder(config.FolderConfiguration{ID: "fresh", Path: "XXX"})
  420. ignores, _, err = m.GetIgnores("fresh")
  421. if err != nil {
  422. t.Error(err)
  423. }
  424. if len(ignores) > 0 {
  425. t.Errorf("Expected no ignores, got: %v", ignores)
  426. }
  427. }