model_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package model
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. "github.com/calmh/syncthing/protocol"
  7. )
  8. func TestNewModel(t *testing.T) {
  9. m := NewModel("foo")
  10. if m == nil {
  11. t.Fatalf("NewModel returned nil")
  12. }
  13. if len(m.need) > 0 {
  14. t.Errorf("New model should have no Need")
  15. }
  16. if len(m.local) > 0 {
  17. t.Errorf("New model should have no Have")
  18. }
  19. }
  20. var testDataExpected = map[string]File{
  21. "foo": File{
  22. Name: "foo",
  23. Flags: 0644,
  24. Modified: 1384244572,
  25. 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}}},
  26. },
  27. "bar": File{
  28. Name: "bar",
  29. Flags: 0644,
  30. Modified: 1384244579,
  31. 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}}},
  32. },
  33. "baz/quux": File{
  34. Name: "baz/quux",
  35. Flags: 0644,
  36. Modified: 1384244676,
  37. 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}}},
  38. },
  39. }
  40. func TestUpdateLocal(t *testing.T) {
  41. m := NewModel("testdata")
  42. fs := m.Walk(false)
  43. m.ReplaceLocal(fs)
  44. if len(m.need) > 0 {
  45. t.Fatalf("Model with only local data should have no need")
  46. }
  47. if l1, l2 := len(m.local), len(testDataExpected); l1 != l2 {
  48. t.Fatalf("Model len(local) incorrect, %d != %d", l1, l2)
  49. }
  50. if l1, l2 := len(m.global), len(testDataExpected); l1 != l2 {
  51. t.Fatalf("Model len(global) incorrect, %d != %d", l1, l2)
  52. }
  53. for name, file := range testDataExpected {
  54. if f, ok := m.local[name]; ok {
  55. if !reflect.DeepEqual(f, file) {
  56. t.Errorf("Incorrect local\n%v !=\n%v\nfor file %q", f, file, name)
  57. }
  58. } else {
  59. t.Errorf("Missing file %q in local table", name)
  60. }
  61. if f, ok := m.global[name]; ok {
  62. if !reflect.DeepEqual(f, file) {
  63. t.Errorf("Incorrect global\n%v !=\n%v\nfor file %q", f, file, name)
  64. }
  65. } else {
  66. t.Errorf("Missing file %q in global table", name)
  67. }
  68. }
  69. for _, f := range fs {
  70. if hf, ok := m.local[f.Name]; !ok || hf.Modified != f.Modified {
  71. t.Fatalf("Incorrect local for %q", f.Name)
  72. }
  73. if cf, ok := m.global[f.Name]; !ok || cf.Modified != f.Modified {
  74. t.Fatalf("Incorrect global for %q", f.Name)
  75. }
  76. }
  77. }
  78. func TestRemoteUpdateExisting(t *testing.T) {
  79. m := NewModel("testdata")
  80. fs := m.Walk(false)
  81. m.ReplaceLocal(fs)
  82. newFile := protocol.FileInfo{
  83. Name: "foo",
  84. Modified: time.Now().Unix(),
  85. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  86. }
  87. m.Index("42", []protocol.FileInfo{newFile})
  88. if l := len(m.need); l != 1 {
  89. t.Errorf("Model missing Need for one file (%d != 1)", l)
  90. }
  91. }
  92. func TestRemoteAddNew(t *testing.T) {
  93. m := NewModel("testdata")
  94. fs := m.Walk(false)
  95. m.ReplaceLocal(fs)
  96. newFile := protocol.FileInfo{
  97. Name: "a new file",
  98. Modified: time.Now().Unix(),
  99. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  100. }
  101. m.Index("42", []protocol.FileInfo{newFile})
  102. if l1, l2 := len(m.need), 1; l1 != l2 {
  103. t.Errorf("Model len(m.need) incorrect (%d != %d)", l1, l2)
  104. }
  105. }
  106. func TestRemoteUpdateOld(t *testing.T) {
  107. m := NewModel("testdata")
  108. fs := m.Walk(false)
  109. m.ReplaceLocal(fs)
  110. oldTimeStamp := int64(1234)
  111. newFile := protocol.FileInfo{
  112. Name: "foo",
  113. Modified: oldTimeStamp,
  114. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  115. }
  116. m.Index("42", []protocol.FileInfo{newFile})
  117. if l1, l2 := len(m.need), 0; l1 != l2 {
  118. t.Errorf("Model len(need) incorrect (%d != %d)", l1, l2)
  119. }
  120. }
  121. func TestRemoteIndexUpdate(t *testing.T) {
  122. m := NewModel("testdata")
  123. fs := m.Walk(false)
  124. m.ReplaceLocal(fs)
  125. foo := protocol.FileInfo{
  126. Name: "foo",
  127. Modified: time.Now().Unix(),
  128. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  129. }
  130. bar := protocol.FileInfo{
  131. Name: "bar",
  132. Modified: time.Now().Unix(),
  133. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  134. }
  135. m.Index("42", []protocol.FileInfo{foo})
  136. if _, ok := m.need["foo"]; !ok {
  137. t.Error("Model doesn't need 'foo'")
  138. }
  139. m.IndexUpdate("42", []protocol.FileInfo{bar})
  140. if _, ok := m.need["foo"]; !ok {
  141. t.Error("Model doesn't need 'foo'")
  142. }
  143. if _, ok := m.need["bar"]; !ok {
  144. t.Error("Model doesn't need 'bar'")
  145. }
  146. }
  147. func TestDelete(t *testing.T) {
  148. m := NewModel("testdata")
  149. fs := m.Walk(false)
  150. m.ReplaceLocal(fs)
  151. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  152. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  153. }
  154. if l1, l2 := len(m.global), len(fs); l1 != l2 {
  155. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  156. }
  157. ot := time.Now().Unix()
  158. newFile := File{
  159. Name: "a new file",
  160. Modified: ot,
  161. Blocks: []Block{{0, 100, []byte("some hash bytes")}},
  162. }
  163. m.updateLocal(newFile)
  164. if l1, l2 := len(m.local), len(fs)+1; l1 != l2 {
  165. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  166. }
  167. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  168. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  169. }
  170. // The deleted file is kept in the local and global tables and marked as deleted.
  171. m.ReplaceLocal(fs)
  172. if l1, l2 := len(m.local), len(fs)+1; l1 != l2 {
  173. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  174. }
  175. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  176. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  177. }
  178. if m.local["a new file"].Flags&(1<<12) == 0 {
  179. t.Error("Unexpected deleted flag = 0 in local table")
  180. }
  181. if len(m.local["a new file"].Blocks) != 0 {
  182. t.Error("Unexpected non-zero blocks for deleted file in local")
  183. }
  184. if ft := m.local["a new file"].Modified; ft != ot+1 {
  185. t.Errorf("Unexpected time %d != %d for deleted file in local", ft, ot+1)
  186. }
  187. if m.global["a new file"].Flags&(1<<12) == 0 {
  188. t.Error("Unexpected deleted flag = 0 in global table")
  189. }
  190. if len(m.global["a new file"].Blocks) != 0 {
  191. t.Error("Unexpected non-zero blocks for deleted file in global")
  192. }
  193. if ft := m.local["a new file"].Modified; ft != ot+1 {
  194. t.Errorf("Unexpected time %d != %d for deleted file in local", ft, ot+1)
  195. }
  196. // Another update should change nothing
  197. m.ReplaceLocal(fs)
  198. if l1, l2 := len(m.local), len(fs)+1; l1 != l2 {
  199. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  200. }
  201. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  202. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  203. }
  204. if m.local["a new file"].Flags&(1<<12) == 0 {
  205. t.Error("Unexpected deleted flag = 0 in local table")
  206. }
  207. if len(m.local["a new file"].Blocks) != 0 {
  208. t.Error("Unexpected non-zero blocks for deleted file in local")
  209. }
  210. if ft := m.local["a new file"].Modified; ft != ot+1 {
  211. t.Errorf("Unexpected time %d != %d for deleted file in local", ft, ot+1)
  212. }
  213. if m.global["a new file"].Flags&(1<<12) == 0 {
  214. t.Error("Unexpected deleted flag = 0 in global table")
  215. }
  216. if len(m.global["a new file"].Blocks) != 0 {
  217. t.Error("Unexpected non-zero blocks for deleted file in global")
  218. }
  219. if ft := m.local["a new file"].Modified; ft != ot+1 {
  220. t.Errorf("Unexpected time %d != %d for deleted file in local", ft, ot+1)
  221. }
  222. }
  223. func TestForgetNode(t *testing.T) {
  224. m := NewModel("testdata")
  225. fs := m.Walk(false)
  226. m.ReplaceLocal(fs)
  227. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  228. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  229. }
  230. if l1, l2 := len(m.global), len(fs); l1 != l2 {
  231. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  232. }
  233. if l1, l2 := len(m.need), 0; l1 != l2 {
  234. t.Errorf("Model len(need) incorrect (%d != %d)", l1, l2)
  235. }
  236. newFile := protocol.FileInfo{
  237. Name: "new file",
  238. Modified: time.Now().Unix(),
  239. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  240. }
  241. m.Index("42", []protocol.FileInfo{newFile})
  242. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  243. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  244. }
  245. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  246. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  247. }
  248. if l1, l2 := len(m.need), 1; l1 != l2 {
  249. t.Errorf("Model len(need) incorrect (%d != %d)", l1, l2)
  250. }
  251. m.Close("42", nil)
  252. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  253. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  254. }
  255. if l1, l2 := len(m.global), len(fs); l1 != l2 {
  256. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  257. }
  258. if l1, l2 := len(m.need), 0; l1 != l2 {
  259. t.Errorf("Model len(need) incorrect (%d != %d)", l1, l2)
  260. }
  261. }