model_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  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. cfg := config.New(device1)
  223. cfg.Devices = []config.DeviceConfiguration{
  224. {
  225. DeviceID: device1,
  226. },
  227. }
  228. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  229. m := NewModel(config.Wrap("/tmp/test", cfg), "device", "syncthing", "dev", db)
  230. if cfg.Devices[0].Name != "" {
  231. t.Errorf("Device already has a name")
  232. }
  233. m.ClusterConfig(device1, ccm)
  234. if cfg.Devices[0].Name != "" {
  235. t.Errorf("Device already has a name")
  236. }
  237. ccm.Options = []protocol.Option{
  238. {
  239. Key: "name",
  240. Value: "tester",
  241. },
  242. }
  243. m.ClusterConfig(device1, ccm)
  244. if cfg.Devices[0].Name != "tester" {
  245. t.Errorf("Device did not get a name")
  246. }
  247. ccm.Options[0].Value = "tester2"
  248. m.ClusterConfig(device1, ccm)
  249. if cfg.Devices[0].Name != "tester" {
  250. t.Errorf("Device name got overwritten")
  251. }
  252. }
  253. func TestClusterConfig(t *testing.T) {
  254. cfg := config.New(device1)
  255. cfg.Devices = []config.DeviceConfiguration{
  256. {
  257. DeviceID: device1,
  258. Introducer: true,
  259. },
  260. {
  261. DeviceID: device2,
  262. },
  263. }
  264. cfg.Folders = []config.FolderConfiguration{
  265. {
  266. ID: "folder1",
  267. Devices: []config.FolderDeviceConfiguration{
  268. {DeviceID: device1},
  269. {DeviceID: device2},
  270. },
  271. },
  272. {
  273. ID: "folder2",
  274. Devices: []config.FolderDeviceConfiguration{
  275. {DeviceID: device1},
  276. {DeviceID: device2},
  277. },
  278. },
  279. }
  280. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  281. m := NewModel(config.Wrap("/tmp/test", cfg), "device", "syncthing", "dev", db)
  282. m.AddFolder(cfg.Folders[0])
  283. m.AddFolder(cfg.Folders[1])
  284. cm := m.clusterConfig(device2)
  285. if l := len(cm.Folders); l != 2 {
  286. t.Fatalf("Incorrect number of folders %d != 2", l)
  287. }
  288. r := cm.Folders[0]
  289. if r.ID != "folder1" {
  290. t.Errorf("Incorrect folder %q != folder1", r.ID)
  291. }
  292. if l := len(r.Devices); l != 2 {
  293. t.Errorf("Incorrect number of devices %d != 2", l)
  294. }
  295. if id := r.Devices[0].ID; bytes.Compare(id, device1[:]) != 0 {
  296. t.Errorf("Incorrect device ID %x != %x", id, device1)
  297. }
  298. if r.Devices[0].Flags&protocol.FlagIntroducer == 0 {
  299. t.Error("Device1 should be flagged as Introducer")
  300. }
  301. if id := r.Devices[1].ID; bytes.Compare(id, device2[:]) != 0 {
  302. t.Errorf("Incorrect device ID %x != %x", id, device2)
  303. }
  304. if r.Devices[1].Flags&protocol.FlagIntroducer != 0 {
  305. t.Error("Device2 should not be flagged as Introducer")
  306. }
  307. r = cm.Folders[1]
  308. if r.ID != "folder2" {
  309. t.Errorf("Incorrect folder %q != folder2", r.ID)
  310. }
  311. if l := len(r.Devices); l != 2 {
  312. t.Errorf("Incorrect number of devices %d != 2", l)
  313. }
  314. if id := r.Devices[0].ID; bytes.Compare(id, device1[:]) != 0 {
  315. t.Errorf("Incorrect device ID %x != %x", id, device1)
  316. }
  317. if r.Devices[0].Flags&protocol.FlagIntroducer == 0 {
  318. t.Error("Device1 should be flagged as Introducer")
  319. }
  320. if id := r.Devices[1].ID; bytes.Compare(id, device2[:]) != 0 {
  321. t.Errorf("Incorrect device ID %x != %x", id, device2)
  322. }
  323. if r.Devices[1].Flags&protocol.FlagIntroducer != 0 {
  324. t.Error("Device2 should not be flagged as Introducer")
  325. }
  326. }
  327. func TestIgnores(t *testing.T) {
  328. arrEqual := func(a, b []string) bool {
  329. if len(a) != len(b) {
  330. return false
  331. }
  332. for i := range a {
  333. if a[i] != b[i] {
  334. return false
  335. }
  336. }
  337. return true
  338. }
  339. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  340. m := NewModel(config.Wrap("", config.Configuration{}), "device", "syncthing", "dev", db)
  341. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata"})
  342. expected := []string{
  343. ".*",
  344. "quux",
  345. }
  346. ignores, err := m.GetIgnores("default")
  347. if err != nil {
  348. t.Error(err)
  349. }
  350. if !arrEqual(ignores, expected) {
  351. t.Errorf("Incorrect ignores: %v != %v", ignores, expected)
  352. }
  353. ignores = append(ignores, "pox")
  354. err = m.SetIgnores("default", ignores)
  355. if err != nil {
  356. t.Error(err)
  357. }
  358. ignores2, err := m.GetIgnores("default")
  359. if err != nil {
  360. t.Error(err)
  361. }
  362. if arrEqual(expected, ignores2) {
  363. t.Errorf("Incorrect ignores: %v == %v", ignores2, expected)
  364. }
  365. if !arrEqual(ignores, ignores2) {
  366. t.Errorf("Incorrect ignores: %v != %v", ignores2, ignores)
  367. }
  368. err = m.SetIgnores("default", expected)
  369. if err != nil {
  370. t.Error(err)
  371. }
  372. ignores, err = m.GetIgnores("default")
  373. if err != nil {
  374. t.Error(err)
  375. }
  376. if !arrEqual(ignores, expected) {
  377. t.Errorf("Incorrect ignores: %v != %v", ignores, expected)
  378. }
  379. ignores, err = m.GetIgnores("doesnotexist")
  380. if err == nil {
  381. t.Error("No error")
  382. }
  383. err = m.SetIgnores("doesnotexist", expected)
  384. if err == nil {
  385. t.Error("No error")
  386. }
  387. m.AddFolder(config.FolderConfiguration{ID: "fresh", Path: "XXX"})
  388. ignores, err = m.GetIgnores("fresh")
  389. if err != nil {
  390. t.Error(err)
  391. }
  392. if len(ignores) > 0 {
  393. t.Errorf("Expected no ignores, got: %v", ignores)
  394. }
  395. }