model_test.go 14 KB

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