model_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. 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. "empty": File{
  31. Name: "empty",
  32. Flags: 0,
  33. Modified: 0,
  34. 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}}},
  35. },
  36. "bar": File{
  37. Name: "bar",
  38. Flags: 0,
  39. Modified: 0,
  40. 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}}},
  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", 1e6)
  54. fs, _ := m.Walk(false)
  55. m.ReplaceLocal(fs)
  56. if fs, _ := m.NeedFiles(); len(fs) > 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", 1e6)
  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 fs, _ := m.NeedFiles(); len(fs) != 1 {
  101. t.Errorf("Model missing Need for one file (%d != 1)", len(fs))
  102. }
  103. }
  104. func TestRemoteAddNew(t *testing.T) {
  105. m := NewModel("testdata", 1e6)
  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 fs, _ := m.NeedFiles(); len(fs) != 1 {
  115. t.Errorf("Model len(m.need) incorrect (%d != 1)", len(fs))
  116. }
  117. }
  118. func TestRemoteUpdateOld(t *testing.T) {
  119. m := NewModel("testdata", 1e6)
  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 fs, _ := m.NeedFiles(); len(fs) != 0 {
  130. t.Errorf("Model len(need) incorrect (%d != 0)", len(fs))
  131. }
  132. }
  133. func TestRemoteIndexUpdate(t *testing.T) {
  134. m := NewModel("testdata", 1e6)
  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 fs, _ := m.NeedFiles(); fs[0].Name != "foo" {
  149. t.Error("Model doesn't need 'foo'")
  150. }
  151. m.IndexUpdate("42", []protocol.FileInfo{bar})
  152. if fs, _ := m.NeedFiles(); fs[0].Name != "foo" {
  153. t.Error("Model doesn't need 'foo'")
  154. }
  155. if fs, _ := m.NeedFiles(); fs[1].Name != "bar" {
  156. t.Error("Model doesn't need 'bar'")
  157. }
  158. }
  159. func TestDelete(t *testing.T) {
  160. m := NewModel("testdata", 1e6)
  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", 1e6)
  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 fs, _ := m.NeedFiles(); len(fs) != 0 {
  258. t.Errorf("Model len(need) incorrect (%d != 0)", len(fs))
  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. newFile = protocol.FileInfo{
  267. Name: "new file 2",
  268. Modified: time.Now().Unix(),
  269. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  270. }
  271. m.Index("43", []protocol.FileInfo{newFile})
  272. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  273. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  274. }
  275. if l1, l2 := len(m.global), len(fs)+2; l1 != l2 {
  276. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  277. }
  278. if fs, _ := m.NeedFiles(); len(fs) != 2 {
  279. t.Errorf("Model len(need) incorrect (%d != 2)", len(fs))
  280. }
  281. m.Close("42", nil)
  282. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  283. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  284. }
  285. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  286. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  287. }
  288. if fs, _ := m.NeedFiles(); len(fs) != 1 {
  289. t.Errorf("Model len(need) incorrect (%d != 1)", len(fs))
  290. }
  291. }
  292. func TestRequest(t *testing.T) {
  293. m := NewModel("testdata", 1e6)
  294. fs, _ := m.Walk(false)
  295. m.ReplaceLocal(fs)
  296. bs, err := m.Request("some node", "foo", 0, 6, nil)
  297. if err != nil {
  298. t.Fatal(err)
  299. }
  300. if bytes.Compare(bs, []byte("foobar")) != 0 {
  301. t.Errorf("Incorrect data from request: %q", string(bs))
  302. }
  303. bs, err = m.Request("some node", "../walk.go", 0, 6, nil)
  304. if err == nil {
  305. t.Error("Unexpected nil error on insecure file read")
  306. }
  307. if bs != nil {
  308. t.Errorf("Unexpected non nil data on insecure file read: %q", string(bs))
  309. }
  310. }
  311. func TestIgnoreWithUnknownFlags(t *testing.T) {
  312. m := NewModel("testdata", 1e6)
  313. fs, _ := m.Walk(false)
  314. m.ReplaceLocal(fs)
  315. valid := protocol.FileInfo{
  316. Name: "valid",
  317. Modified: time.Now().Unix(),
  318. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  319. Flags: protocol.FlagDeleted | 0755,
  320. }
  321. invalid := protocol.FileInfo{
  322. Name: "invalid",
  323. Modified: time.Now().Unix(),
  324. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  325. Flags: 1<<27 | protocol.FlagDeleted | 0755,
  326. }
  327. m.Index("42", []protocol.FileInfo{valid, invalid})
  328. if _, ok := m.global[valid.Name]; !ok {
  329. t.Error("Model should include", valid)
  330. }
  331. if _, ok := m.global[invalid.Name]; ok {
  332. t.Error("Model not should include", invalid)
  333. }
  334. }
  335. func genFiles(n int) []protocol.FileInfo {
  336. files := make([]protocol.FileInfo, n)
  337. t := time.Now().Unix()
  338. for i := 0; i < n; i++ {
  339. files[i] = protocol.FileInfo{
  340. Name: fmt.Sprintf("file%d", i),
  341. Modified: t,
  342. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  343. }
  344. }
  345. return files
  346. }
  347. func BenchmarkIndex10000(b *testing.B) {
  348. m := NewModel("testdata", 1e6)
  349. fs, _ := m.Walk(false)
  350. m.ReplaceLocal(fs)
  351. files := genFiles(10000)
  352. b.ResetTimer()
  353. for i := 0; i < b.N; i++ {
  354. m.Index("42", files)
  355. }
  356. }
  357. func BenchmarkIndex00100(b *testing.B) {
  358. m := NewModel("testdata", 1e6)
  359. fs, _ := m.Walk(false)
  360. m.ReplaceLocal(fs)
  361. files := genFiles(100)
  362. b.ResetTimer()
  363. for i := 0; i < b.N; i++ {
  364. m.Index("42", files)
  365. }
  366. }
  367. func BenchmarkIndexUpdate10000f10000(b *testing.B) {
  368. m := NewModel("testdata", 1e6)
  369. fs, _ := m.Walk(false)
  370. m.ReplaceLocal(fs)
  371. files := genFiles(10000)
  372. m.Index("42", files)
  373. b.ResetTimer()
  374. for i := 0; i < b.N; i++ {
  375. m.IndexUpdate("42", files)
  376. }
  377. }
  378. func BenchmarkIndexUpdate10000f00100(b *testing.B) {
  379. m := NewModel("testdata", 1e6)
  380. fs, _ := m.Walk(false)
  381. m.ReplaceLocal(fs)
  382. files := genFiles(10000)
  383. m.Index("42", files)
  384. ufiles := genFiles(100)
  385. b.ResetTimer()
  386. for i := 0; i < b.N; i++ {
  387. m.IndexUpdate("42", ufiles)
  388. }
  389. }
  390. func BenchmarkIndexUpdate10000f00001(b *testing.B) {
  391. m := NewModel("testdata", 1e6)
  392. fs, _ := m.Walk(false)
  393. m.ReplaceLocal(fs)
  394. files := genFiles(10000)
  395. m.Index("42", files)
  396. ufiles := genFiles(1)
  397. b.ResetTimer()
  398. for i := 0; i < b.N; i++ {
  399. m.IndexUpdate("42", ufiles)
  400. }
  401. }
  402. type FakeConnection struct {
  403. id string
  404. requestData []byte
  405. }
  406. func (FakeConnection) Close() error {
  407. return nil
  408. }
  409. func (f FakeConnection) ID() string {
  410. return string(f.id)
  411. }
  412. func (f FakeConnection) Option(string) string {
  413. return ""
  414. }
  415. func (FakeConnection) Index([]protocol.FileInfo) {}
  416. func (f FakeConnection) Request(name string, offset int64, size uint32, hash []byte) ([]byte, error) {
  417. return f.requestData, nil
  418. }
  419. func (FakeConnection) Ping() bool {
  420. return true
  421. }
  422. func (FakeConnection) Statistics() protocol.Statistics {
  423. return protocol.Statistics{}
  424. }
  425. func BenchmarkRequest(b *testing.B) {
  426. m := NewModel("testdata", 1e6)
  427. fs, _ := m.Walk(false)
  428. m.ReplaceLocal(fs)
  429. const n = 1000
  430. files := make([]protocol.FileInfo, n)
  431. t := time.Now().Unix()
  432. for i := 0; i < n; i++ {
  433. files[i] = protocol.FileInfo{
  434. Name: fmt.Sprintf("file%d", i),
  435. Modified: t,
  436. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  437. }
  438. }
  439. fc := FakeConnection{
  440. id: "42",
  441. requestData: []byte("some data to return"),
  442. }
  443. m.AddConnection(fc, fc)
  444. m.Index("42", files)
  445. b.ResetTimer()
  446. for i := 0; i < b.N; i++ {
  447. data, err := m.requestGlobal("42", files[i%n].Name, 0, 32, nil)
  448. if err != nil {
  449. b.Error(err)
  450. }
  451. if data == nil {
  452. b.Error("nil data")
  453. }
  454. }
  455. }