model_test.go 10 KB

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