model_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. package model
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "reflect"
  7. "testing"
  8. "time"
  9. "github.com/calmh/syncthing/protocol"
  10. )
  11. func TestNewModel(t *testing.T) {
  12. m := NewModel("foo", 1e6)
  13. if m == nil {
  14. t.Fatalf("NewModel returned nil")
  15. }
  16. if fs, _ := m.NeedFiles(); len(fs) > 0 {
  17. t.Errorf("New model should have no Need")
  18. }
  19. if len(m.local) > 0 {
  20. t.Errorf("New model should have no Have")
  21. }
  22. }
  23. var testDataExpected = map[string]File{
  24. "foo": File{
  25. Name: "foo",
  26. Flags: 0,
  27. Modified: 0,
  28. Blocks: []Block{{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}}},
  29. },
  30. "bar": File{
  31. Name: "bar",
  32. Flags: 0,
  33. Modified: 0,
  34. Blocks: []Block{{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}}},
  35. },
  36. }
  37. func init() {
  38. // Fix expected test data to match reality
  39. for n, f := range testDataExpected {
  40. fi, _ := os.Stat("testdata/" + n)
  41. f.Flags = uint32(fi.Mode())
  42. f.Modified = fi.ModTime().Unix()
  43. testDataExpected[n] = f
  44. }
  45. }
  46. func TestUpdateLocal(t *testing.T) {
  47. m := NewModel("testdata", 1e6)
  48. fs, _ := m.Walk(false)
  49. m.ReplaceLocal(fs)
  50. if fs, _ := m.NeedFiles(); len(fs) > 0 {
  51. t.Fatalf("Model with only local data should have no need")
  52. }
  53. if l1, l2 := len(m.local), len(testDataExpected); l1 != l2 {
  54. t.Fatalf("Model len(local) incorrect, %d != %d", l1, l2)
  55. }
  56. if l1, l2 := len(m.global), len(testDataExpected); l1 != l2 {
  57. t.Fatalf("Model len(global) incorrect, %d != %d", l1, l2)
  58. }
  59. for name, file := range testDataExpected {
  60. if f, ok := m.local[name]; ok {
  61. if !reflect.DeepEqual(f, file) {
  62. t.Errorf("Incorrect local\n%v !=\n%v\nfor file %q", f, file, name)
  63. }
  64. } else {
  65. t.Errorf("Missing file %q in local table", name)
  66. }
  67. if f, ok := m.global[name]; ok {
  68. if !reflect.DeepEqual(f, file) {
  69. t.Errorf("Incorrect global\n%v !=\n%v\nfor file %q", f, file, name)
  70. }
  71. } else {
  72. t.Errorf("Missing file %q in global table", name)
  73. }
  74. }
  75. for _, f := range fs {
  76. if hf, ok := m.local[f.Name]; !ok || hf.Modified != f.Modified {
  77. t.Fatalf("Incorrect local for %q", f.Name)
  78. }
  79. if cf, ok := m.global[f.Name]; !ok || cf.Modified != f.Modified {
  80. t.Fatalf("Incorrect global for %q", f.Name)
  81. }
  82. }
  83. }
  84. func TestRemoteUpdateExisting(t *testing.T) {
  85. m := NewModel("testdata", 1e6)
  86. fs, _ := m.Walk(false)
  87. m.ReplaceLocal(fs)
  88. newFile := protocol.FileInfo{
  89. Name: "foo",
  90. Modified: time.Now().Unix(),
  91. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  92. }
  93. m.Index("42", []protocol.FileInfo{newFile})
  94. if fs, _ := m.NeedFiles(); len(fs) != 1 {
  95. t.Errorf("Model missing Need for one file (%d != 1)", len(fs))
  96. }
  97. }
  98. func TestRemoteAddNew(t *testing.T) {
  99. m := NewModel("testdata", 1e6)
  100. fs, _ := m.Walk(false)
  101. m.ReplaceLocal(fs)
  102. newFile := protocol.FileInfo{
  103. Name: "a new file",
  104. Modified: time.Now().Unix(),
  105. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  106. }
  107. m.Index("42", []protocol.FileInfo{newFile})
  108. if fs, _ := m.NeedFiles(); len(fs) != 1 {
  109. t.Errorf("Model len(m.need) incorrect (%d != 1)", len(fs))
  110. }
  111. }
  112. func TestRemoteUpdateOld(t *testing.T) {
  113. m := NewModel("testdata", 1e6)
  114. fs, _ := m.Walk(false)
  115. m.ReplaceLocal(fs)
  116. oldTimeStamp := int64(1234)
  117. newFile := protocol.FileInfo{
  118. Name: "foo",
  119. Modified: oldTimeStamp,
  120. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  121. }
  122. m.Index("42", []protocol.FileInfo{newFile})
  123. if fs, _ := m.NeedFiles(); len(fs) != 0 {
  124. t.Errorf("Model len(need) incorrect (%d != 0)", len(fs))
  125. }
  126. }
  127. func TestRemoteIndexUpdate(t *testing.T) {
  128. m := NewModel("testdata", 1e6)
  129. fs, _ := m.Walk(false)
  130. m.ReplaceLocal(fs)
  131. foo := protocol.FileInfo{
  132. Name: "foo",
  133. Modified: time.Now().Unix(),
  134. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  135. }
  136. bar := protocol.FileInfo{
  137. Name: "bar",
  138. Modified: time.Now().Unix(),
  139. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  140. }
  141. m.Index("42", []protocol.FileInfo{foo})
  142. if fs, _ := m.NeedFiles(); fs[0].Name != "foo" {
  143. t.Error("Model doesn't need 'foo'")
  144. }
  145. m.IndexUpdate("42", []protocol.FileInfo{bar})
  146. if fs, _ := m.NeedFiles(); fs[0].Name != "foo" {
  147. t.Error("Model doesn't need 'foo'")
  148. }
  149. if fs, _ := m.NeedFiles(); fs[1].Name != "bar" {
  150. t.Error("Model doesn't need 'bar'")
  151. }
  152. }
  153. func TestDelete(t *testing.T) {
  154. m := NewModel("testdata", 1e6)
  155. fs, _ := m.Walk(false)
  156. m.ReplaceLocal(fs)
  157. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  158. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  159. }
  160. if l1, l2 := len(m.global), len(fs); l1 != l2 {
  161. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  162. }
  163. ot := time.Now().Unix()
  164. newFile := File{
  165. Name: "a new file",
  166. Modified: ot,
  167. Blocks: []Block{{0, 100, []byte("some hash bytes")}},
  168. }
  169. m.updateLocal(newFile)
  170. if l1, l2 := len(m.local), len(fs)+1; l1 != l2 {
  171. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  172. }
  173. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  174. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  175. }
  176. // The deleted file is kept in the local and global tables and marked as deleted.
  177. m.ReplaceLocal(fs)
  178. if l1, l2 := len(m.local), len(fs)+1; l1 != l2 {
  179. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  180. }
  181. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  182. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  183. }
  184. if m.local["a new file"].Flags&(1<<12) == 0 {
  185. t.Error("Unexpected deleted flag = 0 in local table")
  186. }
  187. if len(m.local["a new file"].Blocks) != 0 {
  188. t.Error("Unexpected non-zero blocks for deleted file in local")
  189. }
  190. if ft := m.local["a new file"].Modified; ft != ot {
  191. t.Errorf("Unexpected time %d != %d for deleted file in local", ft, ot+1)
  192. }
  193. if fv := m.local["a new file"].Version; fv != 1 {
  194. t.Errorf("Unexpected version %d != 1 for deleted file in local", fv)
  195. }
  196. if m.global["a new file"].Flags&(1<<12) == 0 {
  197. t.Error("Unexpected deleted flag = 0 in global table")
  198. }
  199. if len(m.global["a new file"].Blocks) != 0 {
  200. t.Error("Unexpected non-zero blocks for deleted file in global")
  201. }
  202. if ft := m.global["a new file"].Modified; ft != ot {
  203. t.Errorf("Unexpected time %d != %d for deleted file in global", ft, ot+1)
  204. }
  205. if fv := m.local["a new file"].Version; fv != 1 {
  206. t.Errorf("Unexpected version %d != 1 for deleted file in global", fv)
  207. }
  208. // Another update should change nothing
  209. m.ReplaceLocal(fs)
  210. if l1, l2 := len(m.local), len(fs)+1; l1 != l2 {
  211. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  212. }
  213. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  214. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  215. }
  216. if m.local["a new file"].Flags&(1<<12) == 0 {
  217. t.Error("Unexpected deleted flag = 0 in local table")
  218. }
  219. if len(m.local["a new file"].Blocks) != 0 {
  220. t.Error("Unexpected non-zero blocks for deleted file in local")
  221. }
  222. if ft := m.local["a new file"].Modified; ft != ot {
  223. t.Errorf("Unexpected time %d != %d for deleted file in local", ft, ot)
  224. }
  225. if fv := m.local["a new file"].Version; fv != 1 {
  226. t.Errorf("Unexpected version %d != 1 for deleted file in local", fv)
  227. }
  228. if m.global["a new file"].Flags&(1<<12) == 0 {
  229. t.Error("Unexpected deleted flag = 0 in global table")
  230. }
  231. if len(m.global["a new file"].Blocks) != 0 {
  232. t.Error("Unexpected non-zero blocks for deleted file in global")
  233. }
  234. if ft := m.global["a new file"].Modified; ft != ot {
  235. t.Errorf("Unexpected time %d != %d for deleted file in global", ft, ot)
  236. }
  237. if fv := m.local["a new file"].Version; fv != 1 {
  238. t.Errorf("Unexpected version %d != 1 for deleted file in global", fv)
  239. }
  240. }
  241. func TestForgetNode(t *testing.T) {
  242. m := NewModel("testdata", 1e6)
  243. fs, _ := m.Walk(false)
  244. m.ReplaceLocal(fs)
  245. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  246. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  247. }
  248. if l1, l2 := len(m.global), len(fs); l1 != l2 {
  249. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  250. }
  251. if fs, _ := m.NeedFiles(); len(fs) != 0 {
  252. t.Errorf("Model len(need) incorrect (%d != 0)", len(fs))
  253. }
  254. newFile := protocol.FileInfo{
  255. Name: "new file",
  256. Modified: time.Now().Unix(),
  257. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  258. }
  259. m.Index("42", []protocol.FileInfo{newFile})
  260. newFile = protocol.FileInfo{
  261. Name: "new file 2",
  262. Modified: time.Now().Unix(),
  263. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  264. }
  265. m.Index("43", []protocol.FileInfo{newFile})
  266. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  267. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  268. }
  269. if l1, l2 := len(m.global), len(fs)+2; l1 != l2 {
  270. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  271. }
  272. if fs, _ := m.NeedFiles(); len(fs) != 2 {
  273. t.Errorf("Model len(need) incorrect (%d != 2)", len(fs))
  274. }
  275. m.Close("42", nil)
  276. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  277. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  278. }
  279. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  280. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  281. }
  282. if fs, _ := m.NeedFiles(); len(fs) != 1 {
  283. t.Errorf("Model len(need) incorrect (%d != 1)", len(fs))
  284. }
  285. }
  286. func TestRequest(t *testing.T) {
  287. m := NewModel("testdata", 1e6)
  288. fs, _ := m.Walk(false)
  289. m.ReplaceLocal(fs)
  290. bs, err := m.Request("some node", "foo", 0, 6, nil)
  291. if err != nil {
  292. t.Fatal(err)
  293. }
  294. if bytes.Compare(bs, []byte("foobar")) != 0 {
  295. t.Errorf("Incorrect data from request: %q", string(bs))
  296. }
  297. bs, err = m.Request("some node", "../walk.go", 0, 6, nil)
  298. if err == nil {
  299. t.Error("Unexpected nil error on insecure file read")
  300. }
  301. if bs != nil {
  302. t.Errorf("Unexpected non nil data on insecure file read: %q", string(bs))
  303. }
  304. }
  305. func TestIgnoreWithUnknownFlags(t *testing.T) {
  306. m := NewModel("testdata", 1e6)
  307. fs, _ := m.Walk(false)
  308. m.ReplaceLocal(fs)
  309. valid := protocol.FileInfo{
  310. Name: "valid",
  311. Modified: time.Now().Unix(),
  312. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  313. Flags: protocol.FlagDeleted | 0755,
  314. }
  315. invalid := protocol.FileInfo{
  316. Name: "invalid",
  317. Modified: time.Now().Unix(),
  318. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  319. Flags: 1<<27 | protocol.FlagDeleted | 0755,
  320. }
  321. m.Index("42", []protocol.FileInfo{valid, invalid})
  322. if _, ok := m.global[valid.Name]; !ok {
  323. t.Error("Model should include", valid)
  324. }
  325. if _, ok := m.global[invalid.Name]; ok {
  326. t.Error("Model not should include", invalid)
  327. }
  328. }
  329. func prepareModel(n int, m *Model) []protocol.FileInfo {
  330. fs, _ := m.Walk(false)
  331. m.ReplaceLocal(fs)
  332. files := make([]protocol.FileInfo, n)
  333. t := time.Now().Unix()
  334. for i := 0; i < n; i++ {
  335. files[i] = protocol.FileInfo{
  336. Name: fmt.Sprintf("file%d", i),
  337. Modified: t,
  338. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  339. }
  340. }
  341. m.Index("42", files)
  342. return files
  343. }
  344. func BenchmarkRecomputeGlobal10k(b *testing.B) {
  345. m := NewModel("testdata", 1e6)
  346. prepareModel(10000, m)
  347. b.ResetTimer()
  348. for i := 0; i < b.N; i++ {
  349. m.recomputeGlobal()
  350. }
  351. }
  352. func BenchmarkRecomputeNeed10K(b *testing.B) {
  353. m := NewModel("testdata", 1e6)
  354. prepareModel(10000, m)
  355. b.ResetTimer()
  356. for i := 0; i < b.N; i++ {
  357. m.recomputeNeed()
  358. }
  359. }
  360. func BenchmarkIndexUpdate10000(b *testing.B) {
  361. m := NewModel("testdata", 1e6)
  362. files := prepareModel(10000, m)
  363. b.ResetTimer()
  364. for i := 0; i < b.N; i++ {
  365. m.IndexUpdate("42", files)
  366. }
  367. }
  368. type FakeConnection struct {
  369. id string
  370. requestData []byte
  371. }
  372. func (FakeConnection) Close() error {
  373. return nil
  374. }
  375. func (f FakeConnection) ID() string {
  376. return string(f.id)
  377. }
  378. func (f FakeConnection) Option(string) string {
  379. return ""
  380. }
  381. func (FakeConnection) Index([]protocol.FileInfo) {}
  382. func (f FakeConnection) Request(name string, offset int64, size uint32, hash []byte) ([]byte, error) {
  383. return f.requestData, nil
  384. }
  385. func (FakeConnection) Ping() bool {
  386. return true
  387. }
  388. func (FakeConnection) Statistics() protocol.Statistics {
  389. return protocol.Statistics{}
  390. }
  391. func BenchmarkRequest(b *testing.B) {
  392. m := NewModel("testdata", 1e6)
  393. fs, _ := m.Walk(false)
  394. m.ReplaceLocal(fs)
  395. const n = 1000
  396. files := make([]protocol.FileInfo, n)
  397. t := time.Now().Unix()
  398. for i := 0; i < n; i++ {
  399. files[i] = protocol.FileInfo{
  400. Name: fmt.Sprintf("file%d", i),
  401. Modified: t,
  402. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  403. }
  404. }
  405. fc := FakeConnection{
  406. id: "42",
  407. requestData: []byte("some data to return"),
  408. }
  409. m.AddConnection(fc, fc)
  410. m.Index("42", files)
  411. b.ResetTimer()
  412. for i := 0; i < b.N; i++ {
  413. data, err := m.requestGlobal("42", files[i%n].Name, 0, 32, nil)
  414. if err != nil {
  415. b.Error(err)
  416. }
  417. if data == nil {
  418. b.Error("nil data")
  419. }
  420. }
  421. }