model_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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")
  13. if m == nil {
  14. t.Fatalf("NewModel returned nil")
  15. }
  16. if len(m.need) > 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, Length: 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, Length: 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. "baz/quux": File{
  37. Name: "baz/quux",
  38. Flags: 0,
  39. Modified: 0,
  40. Blocks: []Block{{Offset: 0x0, Length: 0x9, Hash: []uint8{0xc1, 0x54, 0xd9, 0x4e, 0x94, 0xba, 0x72, 0x98, 0xa6, 0xad, 0xb0, 0x52, 0x3a, 0xfe, 0x34, 0xd1, 0xb6, 0xa5, 0x81, 0xd6, 0xb8, 0x93, 0xa7, 0x63, 0xd4, 0x5d, 0xdc, 0x5e, 0x20, 0x9d, 0xcb, 0x83}}},
  41. },
  42. }
  43. func init() {
  44. // Fix expected test data to match reality
  45. for n, f := range testDataExpected {
  46. fi, _ := os.Stat("testdata/" + n)
  47. f.Flags = uint32(fi.Mode())
  48. f.Modified = fi.ModTime().Unix()
  49. testDataExpected[n] = f
  50. }
  51. }
  52. func TestUpdateLocal(t *testing.T) {
  53. m := NewModel("testdata")
  54. fs, _ := m.Walk(false)
  55. m.ReplaceLocal(fs)
  56. if len(m.need) > 0 {
  57. t.Fatalf("Model with only local data should have no need")
  58. }
  59. if l1, l2 := len(m.local), len(testDataExpected); l1 != l2 {
  60. t.Fatalf("Model len(local) incorrect, %d != %d", l1, l2)
  61. }
  62. if l1, l2 := len(m.global), len(testDataExpected); l1 != l2 {
  63. t.Fatalf("Model len(global) incorrect, %d != %d", l1, l2)
  64. }
  65. for name, file := range testDataExpected {
  66. if f, ok := m.local[name]; ok {
  67. if !reflect.DeepEqual(f, file) {
  68. t.Errorf("Incorrect local\n%v !=\n%v\nfor file %q", f, file, name)
  69. }
  70. } else {
  71. t.Errorf("Missing file %q in local table", name)
  72. }
  73. if f, ok := m.global[name]; ok {
  74. if !reflect.DeepEqual(f, file) {
  75. t.Errorf("Incorrect global\n%v !=\n%v\nfor file %q", f, file, name)
  76. }
  77. } else {
  78. t.Errorf("Missing file %q in global table", name)
  79. }
  80. }
  81. for _, f := range fs {
  82. if hf, ok := m.local[f.Name]; !ok || hf.Modified != f.Modified {
  83. t.Fatalf("Incorrect local for %q", f.Name)
  84. }
  85. if cf, ok := m.global[f.Name]; !ok || cf.Modified != f.Modified {
  86. t.Fatalf("Incorrect global for %q", f.Name)
  87. }
  88. }
  89. }
  90. func TestRemoteUpdateExisting(t *testing.T) {
  91. m := NewModel("testdata")
  92. fs, _ := m.Walk(false)
  93. m.ReplaceLocal(fs)
  94. newFile := protocol.FileInfo{
  95. Name: "foo",
  96. Modified: time.Now().Unix(),
  97. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  98. }
  99. m.Index("42", []protocol.FileInfo{newFile})
  100. if l := len(m.need); l != 1 {
  101. t.Errorf("Model missing Need for one file (%d != 1)", l)
  102. }
  103. }
  104. func TestRemoteAddNew(t *testing.T) {
  105. m := NewModel("testdata")
  106. fs, _ := m.Walk(false)
  107. m.ReplaceLocal(fs)
  108. newFile := protocol.FileInfo{
  109. Name: "a new file",
  110. Modified: time.Now().Unix(),
  111. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  112. }
  113. m.Index("42", []protocol.FileInfo{newFile})
  114. if l1, l2 := len(m.need), 1; l1 != l2 {
  115. t.Errorf("Model len(m.need) incorrect (%d != %d)", l1, l2)
  116. }
  117. }
  118. func TestRemoteUpdateOld(t *testing.T) {
  119. m := NewModel("testdata")
  120. fs, _ := m.Walk(false)
  121. m.ReplaceLocal(fs)
  122. oldTimeStamp := int64(1234)
  123. newFile := protocol.FileInfo{
  124. Name: "foo",
  125. Modified: oldTimeStamp,
  126. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  127. }
  128. m.Index("42", []protocol.FileInfo{newFile})
  129. if l1, l2 := len(m.need), 0; l1 != l2 {
  130. t.Errorf("Model len(need) incorrect (%d != %d)", l1, l2)
  131. }
  132. }
  133. func TestRemoteIndexUpdate(t *testing.T) {
  134. m := NewModel("testdata")
  135. fs, _ := m.Walk(false)
  136. m.ReplaceLocal(fs)
  137. foo := protocol.FileInfo{
  138. Name: "foo",
  139. Modified: time.Now().Unix(),
  140. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  141. }
  142. bar := protocol.FileInfo{
  143. Name: "bar",
  144. Modified: time.Now().Unix(),
  145. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  146. }
  147. m.Index("42", []protocol.FileInfo{foo})
  148. if _, ok := m.need["foo"]; !ok {
  149. t.Error("Model doesn't need 'foo'")
  150. }
  151. m.IndexUpdate("42", []protocol.FileInfo{bar})
  152. if _, ok := m.need["foo"]; !ok {
  153. t.Error("Model doesn't need 'foo'")
  154. }
  155. if _, ok := m.need["bar"]; !ok {
  156. t.Error("Model doesn't need 'bar'")
  157. }
  158. }
  159. func TestDelete(t *testing.T) {
  160. m := NewModel("testdata")
  161. fs, _ := m.Walk(false)
  162. m.ReplaceLocal(fs)
  163. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  164. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  165. }
  166. if l1, l2 := len(m.global), len(fs); l1 != l2 {
  167. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  168. }
  169. ot := time.Now().Unix()
  170. newFile := File{
  171. Name: "a new file",
  172. Modified: ot,
  173. Blocks: []Block{{0, 100, []byte("some hash bytes")}},
  174. }
  175. m.updateLocal(newFile)
  176. if l1, l2 := len(m.local), len(fs)+1; l1 != l2 {
  177. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  178. }
  179. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  180. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  181. }
  182. // The deleted file is kept in the local and global tables and marked as deleted.
  183. m.ReplaceLocal(fs)
  184. if l1, l2 := len(m.local), len(fs)+1; l1 != l2 {
  185. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  186. }
  187. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  188. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  189. }
  190. if m.local["a new file"].Flags&(1<<12) == 0 {
  191. t.Error("Unexpected deleted flag = 0 in local table")
  192. }
  193. if len(m.local["a new file"].Blocks) != 0 {
  194. t.Error("Unexpected non-zero blocks for deleted file in local")
  195. }
  196. if ft := m.local["a new file"].Modified; ft != ot {
  197. t.Errorf("Unexpected time %d != %d for deleted file in local", ft, ot+1)
  198. }
  199. if fv := m.local["a new file"].Version; fv != 1 {
  200. t.Errorf("Unexpected version %d != 1 for deleted file in local", fv)
  201. }
  202. if m.global["a new file"].Flags&(1<<12) == 0 {
  203. t.Error("Unexpected deleted flag = 0 in global table")
  204. }
  205. if len(m.global["a new file"].Blocks) != 0 {
  206. t.Error("Unexpected non-zero blocks for deleted file in global")
  207. }
  208. if ft := m.global["a new file"].Modified; ft != ot {
  209. t.Errorf("Unexpected time %d != %d for deleted file in global", ft, ot+1)
  210. }
  211. if fv := m.local["a new file"].Version; fv != 1 {
  212. t.Errorf("Unexpected version %d != 1 for deleted file in global", fv)
  213. }
  214. // Another update should change nothing
  215. m.ReplaceLocal(fs)
  216. if l1, l2 := len(m.local), len(fs)+1; l1 != l2 {
  217. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  218. }
  219. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  220. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  221. }
  222. if m.local["a new file"].Flags&(1<<12) == 0 {
  223. t.Error("Unexpected deleted flag = 0 in local table")
  224. }
  225. if len(m.local["a new file"].Blocks) != 0 {
  226. t.Error("Unexpected non-zero blocks for deleted file in local")
  227. }
  228. if ft := m.local["a new file"].Modified; ft != ot {
  229. t.Errorf("Unexpected time %d != %d for deleted file in local", ft, ot)
  230. }
  231. if fv := m.local["a new file"].Version; fv != 1 {
  232. t.Errorf("Unexpected version %d != 1 for deleted file in local", fv)
  233. }
  234. if m.global["a new file"].Flags&(1<<12) == 0 {
  235. t.Error("Unexpected deleted flag = 0 in global table")
  236. }
  237. if len(m.global["a new file"].Blocks) != 0 {
  238. t.Error("Unexpected non-zero blocks for deleted file in global")
  239. }
  240. if ft := m.global["a new file"].Modified; ft != ot {
  241. t.Errorf("Unexpected time %d != %d for deleted file in global", ft, ot)
  242. }
  243. if fv := m.local["a new file"].Version; fv != 1 {
  244. t.Errorf("Unexpected version %d != 1 for deleted file in global", fv)
  245. }
  246. }
  247. func TestForgetNode(t *testing.T) {
  248. m := NewModel("testdata")
  249. fs, _ := m.Walk(false)
  250. m.ReplaceLocal(fs)
  251. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  252. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  253. }
  254. if l1, l2 := len(m.global), len(fs); l1 != l2 {
  255. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  256. }
  257. if l1, l2 := len(m.need), 0; l1 != l2 {
  258. t.Errorf("Model len(need) incorrect (%d != %d)", l1, l2)
  259. }
  260. newFile := protocol.FileInfo{
  261. Name: "new file",
  262. Modified: time.Now().Unix(),
  263. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  264. }
  265. m.Index("42", []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)+1; l1 != l2 {
  270. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  271. }
  272. if l1, l2 := len(m.need), 1; l1 != l2 {
  273. t.Errorf("Model len(need) incorrect (%d != %d)", l1, l2)
  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); l1 != l2 {
  280. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  281. }
  282. if l1, l2 := len(m.need), 0; l1 != l2 {
  283. t.Errorf("Model len(need) incorrect (%d != %d)", l1, l2)
  284. }
  285. }
  286. func TestRequest(t *testing.T) {
  287. m := NewModel("testdata")
  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 TestSuppression(t *testing.T) {
  306. var testdata = []struct {
  307. lastChange time.Time
  308. hold int
  309. result bool
  310. }{
  311. {time.Unix(0, 0), 0, false}, // First change
  312. {time.Now().Add(-1 * time.Second), 0, true}, // Changed once one second ago, suppress
  313. {time.Now().Add(-119 * time.Second), 0, true}, // Changed once 119 seconds ago, suppress
  314. {time.Now().Add(-121 * time.Second), 0, false}, // Changed once 121 seconds ago, permit
  315. {time.Now().Add(-179 * time.Second), 1, true}, // Suppressed once 179 seconds ago, suppress again
  316. {time.Now().Add(-181 * time.Second), 1, false}, // Suppressed once 181 seconds ago, permit
  317. {time.Now().Add(-599 * time.Second), 99, true}, // Suppressed lots of times, last allowed 599 seconds ago, suppress again
  318. {time.Now().Add(-601 * time.Second), 99, false}, // Suppressed lots of times, last allowed 601 seconds ago, permit
  319. }
  320. for i, tc := range testdata {
  321. if shouldSuppressChange(tc.lastChange, tc.hold) != tc.result {
  322. t.Errorf("Incorrect result for test #%d: %v", i, tc)
  323. }
  324. }
  325. }
  326. func TestIgnoreWithUnknownFlags(t *testing.T) {
  327. m := NewModel("testdata")
  328. fs, _ := m.Walk(false)
  329. m.ReplaceLocal(fs)
  330. valid := protocol.FileInfo{
  331. Name: "valid",
  332. Modified: time.Now().Unix(),
  333. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  334. Flags: protocol.FlagDeleted | 0755,
  335. }
  336. invalid := protocol.FileInfo{
  337. Name: "invalid",
  338. Modified: time.Now().Unix(),
  339. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  340. Flags: 1<<27 | protocol.FlagDeleted | 0755,
  341. }
  342. m.Index("42", []protocol.FileInfo{valid, invalid})
  343. if _, ok := m.global[valid.Name]; !ok {
  344. t.Error("Model should include", valid)
  345. }
  346. if _, ok := m.global[invalid.Name]; ok {
  347. t.Error("Model not should include", invalid)
  348. }
  349. }
  350. func prepareModel(n int, m *Model) []protocol.FileInfo {
  351. fs, _ := m.Walk(false)
  352. m.ReplaceLocal(fs)
  353. files := make([]protocol.FileInfo, n)
  354. t := time.Now().Unix()
  355. for i := 0; i < n; i++ {
  356. files[i] = protocol.FileInfo{
  357. Name: fmt.Sprintf("file%d", i),
  358. Modified: t,
  359. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  360. }
  361. }
  362. m.Index("42", files)
  363. return files
  364. }
  365. func BenchmarkRecomputeGlobal10k(b *testing.B) {
  366. m := NewModel("testdata")
  367. prepareModel(10000, m)
  368. b.ResetTimer()
  369. for i := 0; i < b.N; i++ {
  370. m.recomputeGlobal()
  371. }
  372. }
  373. func BenchmarkRecomputeNeed10K(b *testing.B) {
  374. m := NewModel("testdata")
  375. prepareModel(10000, m)
  376. b.ResetTimer()
  377. for i := 0; i < b.N; i++ {
  378. m.recomputeNeed()
  379. }
  380. }
  381. func BenchmarkIndexUpdate10000(b *testing.B) {
  382. m := NewModel("testdata")
  383. files := prepareModel(10000, m)
  384. b.ResetTimer()
  385. for i := 0; i < b.N; i++ {
  386. m.IndexUpdate("42", files)
  387. }
  388. }
  389. type FakeConnection struct {
  390. id string
  391. requestData []byte
  392. }
  393. func (FakeConnection) Close() error {
  394. return nil
  395. }
  396. func (f FakeConnection) ID() string {
  397. return string(f.id)
  398. }
  399. func (FakeConnection) Index([]protocol.FileInfo) {}
  400. func (f FakeConnection) Request(name string, offset uint64, size uint32, hash []byte) ([]byte, error) {
  401. return f.requestData, nil
  402. }
  403. func (FakeConnection) Ping() bool {
  404. return true
  405. }
  406. func (FakeConnection) Statistics() protocol.Statistics {
  407. return protocol.Statistics{}
  408. }
  409. func BenchmarkRequest(b *testing.B) {
  410. m := NewModel("testdata")
  411. fs, _ := m.Walk(false)
  412. m.ReplaceLocal(fs)
  413. const n = 1000
  414. files := make([]protocol.FileInfo, n)
  415. t := time.Now().Unix()
  416. for i := 0; i < n; i++ {
  417. files[i] = protocol.FileInfo{
  418. Name: fmt.Sprintf("file%d", i),
  419. Modified: t,
  420. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  421. }
  422. }
  423. fc := FakeConnection{
  424. id: "42",
  425. requestData: []byte("some data to return"),
  426. }
  427. m.AddConnection(fc, fc)
  428. m.Index("42", files)
  429. b.ResetTimer()
  430. for i := 0; i < b.N; i++ {
  431. data, err := m.requestGlobal("42", files[i%n].Name, 0, 32, nil)
  432. if err != nil {
  433. b.Error(err)
  434. }
  435. if data == nil {
  436. b.Error("nil data")
  437. }
  438. }
  439. }