model_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package main
  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("foo")
  42. fs := Walk("testdata", m, 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("foo")
  80. fs := Walk("testdata", m, 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(string("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("foo")
  94. fs := Walk("testdata", m, 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(string("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("foo")
  108. fs := Walk("testdata", m, 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(string("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 TestDelete(t *testing.T) {
  122. m := NewModel("foo")
  123. fs := Walk("testdata", m, false)
  124. m.ReplaceLocal(fs)
  125. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  126. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  127. }
  128. if l1, l2 := len(m.global), len(fs); l1 != l2 {
  129. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  130. }
  131. ot := time.Now().Unix()
  132. newFile := File{
  133. Name: "a new file",
  134. Modified: ot,
  135. Blocks: []Block{{0, 100, []byte("some hash bytes")}},
  136. }
  137. m.UpdateLocal(newFile)
  138. if l1, l2 := len(m.local), len(fs)+1; l1 != l2 {
  139. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  140. }
  141. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  142. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  143. }
  144. // The deleted file is kept in the local and global tables and marked as deleted.
  145. m.ReplaceLocal(fs)
  146. if l1, l2 := len(m.local), len(fs)+1; l1 != l2 {
  147. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  148. }
  149. if l1, l2 := len(m.global), len(fs)+1; l1 != l2 {
  150. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  151. }
  152. if m.local["a new file"].Flags&(1<<12) == 0 {
  153. t.Error("Unexpected deleted flag = 0 in local table")
  154. }
  155. if len(m.local["a new file"].Blocks) != 0 {
  156. t.Error("Unexpected non-zero blocks for deleted file in local")
  157. }
  158. if ft := m.local["a new file"].Modified; ft != ot+1 {
  159. t.Errorf("Unexpected time %d != %d for deleted file in local", ft, ot+1)
  160. }
  161. if m.global["a new file"].Flags&(1<<12) == 0 {
  162. t.Error("Unexpected deleted flag = 0 in global table")
  163. }
  164. if len(m.global["a new file"].Blocks) != 0 {
  165. t.Error("Unexpected non-zero blocks for deleted file in global")
  166. }
  167. if ft := m.local["a new file"].Modified; ft != ot+1 {
  168. t.Errorf("Unexpected time %d != %d for deleted file in local", ft, ot+1)
  169. }
  170. // Another update should change nothing
  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. }
  197. func TestForgetNode(t *testing.T) {
  198. m := NewModel("foo")
  199. fs := Walk("testdata", m, false)
  200. m.ReplaceLocal(fs)
  201. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  202. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  203. }
  204. if l1, l2 := len(m.global), len(fs); l1 != l2 {
  205. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  206. }
  207. if l1, l2 := len(m.need), 0; l1 != l2 {
  208. t.Errorf("Model len(need) incorrect (%d != %d)", l1, l2)
  209. }
  210. newFile := protocol.FileInfo{
  211. Name: "new file",
  212. Modified: time.Now().Unix(),
  213. Blocks: []protocol.BlockInfo{{100, []byte("some hash bytes")}},
  214. }
  215. m.Index(string("42"), []protocol.FileInfo{newFile})
  216. if l1, l2 := len(m.local), len(fs); 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 l1, l2 := len(m.need), 1; l1 != l2 {
  223. t.Errorf("Model len(need) incorrect (%d != %d)", l1, l2)
  224. }
  225. m.Close(string("42"))
  226. if l1, l2 := len(m.local), len(fs); l1 != l2 {
  227. t.Errorf("Model len(local) incorrect (%d != %d)", l1, l2)
  228. }
  229. if l1, l2 := len(m.global), len(fs); l1 != l2 {
  230. t.Errorf("Model len(global) incorrect (%d != %d)", l1, l2)
  231. }
  232. if l1, l2 := len(m.need), 0; l1 != l2 {
  233. t.Errorf("Model len(need) incorrect (%d != %d)", l1, l2)
  234. }
  235. }