model_test.go 12 KB

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