puller_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package model
  16. import (
  17. "os"
  18. "path/filepath"
  19. "testing"
  20. "time"
  21. "github.com/syncthing/syncthing/internal/config"
  22. "github.com/syncthing/syncthing/internal/protocol"
  23. "github.com/syncthing/syncthing/internal/scanner"
  24. "github.com/syndtr/goleveldb/leveldb"
  25. "github.com/syndtr/goleveldb/leveldb/storage"
  26. )
  27. func init() {
  28. // We do this to make sure that the temp file required for the tests does
  29. // not get removed during the tests.
  30. future := time.Now().Add(time.Hour)
  31. err := os.Chtimes(filepath.Join("testdata", defTempNamer.TempName("file")), future, future)
  32. if err != nil {
  33. panic(err)
  34. }
  35. }
  36. var blocks = []protocol.BlockInfo{
  37. {Hash: []uint8{0xfa, 0x43, 0x23, 0x9b, 0xce, 0xe7, 0xb9, 0x7c, 0xa6, 0x2f, 0x0, 0x7c, 0xc6, 0x84, 0x87, 0x56, 0xa, 0x39, 0xe1, 0x9f, 0x74, 0xf3, 0xdd, 0xe7, 0x48, 0x6d, 0xb3, 0xf9, 0x8d, 0xf8, 0xe4, 0x71}}, // Zero'ed out block
  38. {Offset: 0, Size: 0x20000, Hash: []uint8{0x7e, 0xad, 0xbc, 0x36, 0xae, 0xbb, 0xcf, 0x74, 0x43, 0xe2, 0x7a, 0x5a, 0x4b, 0xb8, 0x5b, 0xce, 0xe6, 0x9e, 0x1e, 0x10, 0xf9, 0x8a, 0xbc, 0x77, 0x95, 0x2, 0x29, 0x60, 0x9e, 0x96, 0xae, 0x6c}},
  39. {Offset: 131072, Size: 0x20000, Hash: []uint8{0x3c, 0xc4, 0x20, 0xf4, 0xb, 0x2e, 0xcb, 0xb9, 0x5d, 0xce, 0x34, 0xa8, 0xc3, 0x92, 0xea, 0xf3, 0xda, 0x88, 0x33, 0xee, 0x7a, 0xb6, 0xe, 0xf1, 0x82, 0x5e, 0xb0, 0xa9, 0x26, 0xa9, 0xc0, 0xef}},
  40. {Offset: 262144, Size: 0x20000, Hash: []uint8{0x76, 0xa8, 0xc, 0x69, 0xd7, 0x5c, 0x52, 0xfd, 0xdf, 0x55, 0xef, 0x44, 0xc1, 0xd6, 0x25, 0x48, 0x4d, 0x98, 0x48, 0x4d, 0xaa, 0x50, 0xf6, 0x6b, 0x32, 0x47, 0x55, 0x81, 0x6b, 0xed, 0xee, 0xfb}},
  41. {Offset: 393216, Size: 0x20000, Hash: []uint8{0x44, 0x1e, 0xa4, 0xf2, 0x8d, 0x1f, 0xc3, 0x1b, 0x9d, 0xa5, 0x18, 0x5e, 0x59, 0x1b, 0xd8, 0x5c, 0xba, 0x7d, 0xb9, 0x8d, 0x70, 0x11, 0x5c, 0xea, 0xa1, 0x57, 0x4d, 0xcb, 0x3c, 0x5b, 0xf8, 0x6c}},
  42. {Offset: 524288, Size: 0x20000, Hash: []uint8{0x8, 0x40, 0xd0, 0x5e, 0x80, 0x0, 0x0, 0x7c, 0x8b, 0xb3, 0x8b, 0xf7, 0x7b, 0x23, 0x26, 0x28, 0xab, 0xda, 0xcf, 0x86, 0x8f, 0xc2, 0x8a, 0x39, 0xc6, 0xe6, 0x69, 0x59, 0x97, 0xb6, 0x1a, 0x43}},
  43. {Offset: 655360, Size: 0x20000, Hash: []uint8{0x38, 0x8e, 0x44, 0xcb, 0x30, 0xd8, 0x90, 0xf, 0xce, 0x7, 0x4b, 0x58, 0x86, 0xde, 0xce, 0x59, 0xa2, 0x46, 0xd2, 0xf9, 0xba, 0xaf, 0x35, 0x87, 0x38, 0xdf, 0xd2, 0xd, 0xf9, 0x45, 0xed, 0x91}},
  44. {Offset: 786432, Size: 0x20000, Hash: []uint8{0x32, 0x28, 0xcd, 0xf, 0x37, 0x21, 0xe5, 0xd4, 0x1e, 0x58, 0x87, 0x73, 0x8e, 0x36, 0xdf, 0xb2, 0x70, 0x78, 0x56, 0xc3, 0x42, 0xff, 0xf7, 0x8f, 0x37, 0x95, 0x0, 0x26, 0xa, 0xac, 0x54, 0x72}},
  45. {Offset: 917504, Size: 0x20000, Hash: []uint8{0x96, 0x6b, 0x15, 0x6b, 0xc4, 0xf, 0x19, 0x18, 0xca, 0xbb, 0x5f, 0xd6, 0xbb, 0xa2, 0xc6, 0x2a, 0xac, 0xbb, 0x8a, 0xb9, 0xce, 0xec, 0x4c, 0xdb, 0x78, 0xec, 0x57, 0x5d, 0x33, 0xf9, 0x8e, 0xaf}},
  46. }
  47. // Layout of the files: (indexes from the above array)
  48. // 12345678 - Required file
  49. // 02005008 - Existing file (currently in the index)
  50. // 02340070 - Temp file on the disk
  51. func TestHandleFile(t *testing.T) {
  52. // After the diff between required and existing we should:
  53. // Copy: 2, 5, 8
  54. // Pull: 1, 3, 4, 6, 7
  55. // Create existing file
  56. existingFile := protocol.FileInfo{
  57. Name: "filex",
  58. Flags: 0,
  59. Modified: 0,
  60. Blocks: []protocol.BlockInfo{
  61. blocks[0], blocks[2], blocks[0], blocks[0],
  62. blocks[5], blocks[0], blocks[0], blocks[8],
  63. },
  64. }
  65. // Create target file
  66. requiredFile := existingFile
  67. requiredFile.Blocks = blocks[1:]
  68. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  69. m := NewModel(config.Wrap("/tmp/test", config.Configuration{}), "device", "syncthing", "dev", db)
  70. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata"})
  71. // Update index
  72. m.updateLocal("default", existingFile)
  73. p := Puller{
  74. folder: "default",
  75. dir: "testdata",
  76. model: m,
  77. }
  78. copyChan := make(chan copyBlocksState, 1)
  79. p.handleFile(requiredFile, copyChan, nil)
  80. // Receive the results
  81. toCopy := <-copyChan
  82. if len(toCopy.blocks) != 8 {
  83. t.Errorf("Unexpected count of copy blocks: %d != 8", len(toCopy.blocks))
  84. }
  85. for i, block := range toCopy.blocks {
  86. if string(block.Hash) != string(blocks[i+1].Hash) {
  87. t.Errorf("Block mismatch: %s != %s", block.String(), blocks[i+1].String())
  88. }
  89. }
  90. }
  91. func TestHandleFileWithTemp(t *testing.T) {
  92. // After diff between required and existing we should:
  93. // Copy: 2, 5, 8
  94. // Pull: 1, 3, 4, 6, 7
  95. // After dropping out blocks already on the temp file we should:
  96. // Copy: 5, 8
  97. // Pull: 1, 6
  98. // Create existing file
  99. existingFile := protocol.FileInfo{
  100. Name: "file",
  101. Flags: 0,
  102. Modified: 0,
  103. Blocks: []protocol.BlockInfo{
  104. blocks[0], blocks[2], blocks[0], blocks[0],
  105. blocks[5], blocks[0], blocks[0], blocks[8],
  106. },
  107. }
  108. // Create target file
  109. requiredFile := existingFile
  110. requiredFile.Blocks = blocks[1:]
  111. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  112. m := NewModel(config.Wrap("/tmp/test", config.Configuration{}), "device", "syncthing", "dev", db)
  113. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata"})
  114. // Update index
  115. m.updateLocal("default", existingFile)
  116. p := Puller{
  117. folder: "default",
  118. dir: "testdata",
  119. model: m,
  120. }
  121. copyChan := make(chan copyBlocksState, 1)
  122. p.handleFile(requiredFile, copyChan, nil)
  123. // Receive the results
  124. toCopy := <-copyChan
  125. if len(toCopy.blocks) != 4 {
  126. t.Errorf("Unexpected count of copy blocks: %d != 4", len(toCopy.blocks))
  127. }
  128. for i, eq := range []int{1, 5, 6, 8} {
  129. if string(toCopy.blocks[i].Hash) != string(blocks[eq].Hash) {
  130. t.Errorf("Block mismatch: %s != %s", toCopy.blocks[i].String(), blocks[eq].String())
  131. }
  132. }
  133. }
  134. func TestCopierFinder(t *testing.T) {
  135. // After diff between required and existing we should:
  136. // Copy: 1, 2, 3, 4, 6, 7, 8
  137. // Since there is no existing file, nor a temp file
  138. // After dropping out blocks found locally:
  139. // Pull: 1, 5, 6, 8
  140. tempFile := filepath.Join("testdata", defTempNamer.TempName("file2"))
  141. err := os.Remove(tempFile)
  142. if err != nil && !os.IsNotExist(err) {
  143. t.Error(err)
  144. }
  145. // Create existing file
  146. existingFile := protocol.FileInfo{
  147. Name: defTempNamer.TempName("file"),
  148. Flags: 0,
  149. Modified: 0,
  150. Blocks: []protocol.BlockInfo{
  151. blocks[0], blocks[2], blocks[3], blocks[4],
  152. blocks[0], blocks[0], blocks[7], blocks[0],
  153. },
  154. }
  155. // Create target file
  156. requiredFile := existingFile
  157. requiredFile.Blocks = blocks[1:]
  158. requiredFile.Name = "file2"
  159. fcfg := config.FolderConfiguration{ID: "default", Path: "testdata"}
  160. cfg := config.Configuration{Folders: []config.FolderConfiguration{fcfg}}
  161. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  162. m := NewModel(config.Wrap("/tmp/test", cfg), "device", "syncthing", "dev", db)
  163. m.AddFolder(fcfg)
  164. // Update index
  165. m.updateLocal("default", existingFile)
  166. iterFn := func(folder, file string, index uint32) bool {
  167. return true
  168. }
  169. // Verify that the blocks we say exist on file, really exist in the db.
  170. for _, idx := range []int{2, 3, 4, 7} {
  171. if m.finder.Iterate(blocks[idx].Hash, iterFn) == false {
  172. t.Error("Didn't find block")
  173. }
  174. }
  175. p := Puller{
  176. folder: "default",
  177. dir: "testdata",
  178. model: m,
  179. }
  180. copyChan := make(chan copyBlocksState)
  181. pullChan := make(chan pullBlockState, 4)
  182. finisherChan := make(chan *sharedPullerState, 1)
  183. // Run a single fetcher routine
  184. go p.copierRoutine(copyChan, pullChan, finisherChan)
  185. p.handleFile(requiredFile, copyChan, finisherChan)
  186. pulls := []pullBlockState{<-pullChan, <-pullChan, <-pullChan, <-pullChan}
  187. finish := <-finisherChan
  188. select {
  189. case <-pullChan:
  190. t.Fatal("Finisher channel has data to be read")
  191. case <-finisherChan:
  192. t.Fatal("Finisher channel has data to be read")
  193. default:
  194. }
  195. // Verify that the right blocks went into the pull list
  196. for i, eq := range []int{1, 5, 6, 8} {
  197. if string(pulls[i].block.Hash) != string(blocks[eq].Hash) {
  198. t.Errorf("Block %d mismatch: %s != %s", eq, pulls[i].block.String(), blocks[eq].String())
  199. }
  200. if string(finish.file.Blocks[eq-1].Hash) != string(blocks[eq].Hash) {
  201. t.Errorf("Block %d mismatch: %s != %s", eq, finish.file.Blocks[eq-1].String(), blocks[eq].String())
  202. }
  203. }
  204. // Verify that the fetched blocks have actually been written to the temp file
  205. blks, err := scanner.HashFile(tempFile, protocol.BlockSize)
  206. if err != nil {
  207. t.Log(err)
  208. }
  209. for _, eq := range []int{2, 3, 4, 7} {
  210. if string(blks[eq-1].Hash) != string(blocks[eq].Hash) {
  211. t.Errorf("Block %d mismatch: %s != %s", eq, blks[eq-1].String(), blocks[eq].String())
  212. }
  213. }
  214. finish.fd.Close()
  215. os.Remove(tempFile)
  216. }
  217. // Test that updating a file removes it's old blocks from the blockmap
  218. func TestCopierCleanup(t *testing.T) {
  219. iterFn := func(folder, file string, index uint32) bool {
  220. return true
  221. }
  222. fcfg := config.FolderConfiguration{ID: "default", Path: "testdata"}
  223. cfg := config.Configuration{Folders: []config.FolderConfiguration{fcfg}}
  224. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  225. m := NewModel(config.Wrap("/tmp/test", cfg), "device", "syncthing", "dev", db)
  226. m.AddFolder(fcfg)
  227. // Create a file
  228. file := protocol.FileInfo{
  229. Name: "test",
  230. Flags: 0,
  231. Modified: 0,
  232. Blocks: []protocol.BlockInfo{blocks[0]},
  233. }
  234. // Add file to index
  235. m.updateLocal("default", file)
  236. if !m.finder.Iterate(blocks[0].Hash, iterFn) {
  237. t.Error("Expected block not found")
  238. }
  239. file.Blocks = []protocol.BlockInfo{blocks[1]}
  240. file.Version++
  241. // Update index (removing old blocks)
  242. m.updateLocal("default", file)
  243. if m.finder.Iterate(blocks[0].Hash, iterFn) {
  244. t.Error("Unexpected block found")
  245. }
  246. if !m.finder.Iterate(blocks[1].Hash, iterFn) {
  247. t.Error("Expected block not found")
  248. }
  249. file.Blocks = []protocol.BlockInfo{blocks[0]}
  250. file.Version++
  251. // Update index (removing old blocks)
  252. m.updateLocal("default", file)
  253. if !m.finder.Iterate(blocks[0].Hash, iterFn) {
  254. t.Error("Unexpected block found")
  255. }
  256. if m.finder.Iterate(blocks[1].Hash, iterFn) {
  257. t.Error("Expected block not found")
  258. }
  259. }
  260. // Make sure that the copier routine hashes the content when asked, and pulls
  261. // if it fails to find the block.
  262. func TestLastResortPulling(t *testing.T) {
  263. fcfg := config.FolderConfiguration{ID: "default", Path: "testdata"}
  264. cfg := config.Configuration{Folders: []config.FolderConfiguration{fcfg}}
  265. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  266. m := NewModel(config.Wrap("/tmp/test", cfg), "device", "syncthing", "dev", db)
  267. m.AddFolder(fcfg)
  268. // Add a file to index (with the incorrect block representation, as content
  269. // doesn't actually match the block list)
  270. file := protocol.FileInfo{
  271. Name: "empty",
  272. Flags: 0,
  273. Modified: 0,
  274. Blocks: []protocol.BlockInfo{blocks[0]},
  275. }
  276. m.updateLocal("default", file)
  277. // Pretend that we are handling a new file of the same content but
  278. // with a different name (causing to copy that particular block)
  279. file.Name = "newfile"
  280. iterFn := func(folder, file string, index uint32) bool {
  281. return true
  282. }
  283. // Check that that particular block is there
  284. if !m.finder.Iterate(blocks[0].Hash, iterFn) {
  285. t.Error("Expected block not found")
  286. }
  287. p := Puller{
  288. folder: "default",
  289. dir: "testdata",
  290. model: m,
  291. }
  292. copyChan := make(chan copyBlocksState)
  293. pullChan := make(chan pullBlockState, 1)
  294. finisherChan := make(chan *sharedPullerState, 1)
  295. // Run a single copier routine
  296. go p.copierRoutine(copyChan, pullChan, finisherChan)
  297. p.handleFile(file, copyChan, finisherChan)
  298. // Copier should hash empty file, realise that the region it has read
  299. // doesn't match the hash which was advertised by the block map, fix it
  300. // and ask to pull the block.
  301. <-pullChan
  302. // Verify that it did fix the incorrect hash.
  303. if m.finder.Iterate(blocks[0].Hash, iterFn) {
  304. t.Error("Found unexpected block")
  305. }
  306. if !m.finder.Iterate(scanner.SHA256OfNothing, iterFn) {
  307. t.Error("Expected block not found")
  308. }
  309. (<-finisherChan).fd.Close()
  310. os.Remove(filepath.Join("testdata", defTempNamer.TempName("newfile")))
  311. }