model_test.go 8.7 KB

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