puller_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  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. "testing"
  18. "github.com/syncthing/syncthing/internal/config"
  19. "github.com/syncthing/syncthing/internal/protocol"
  20. "github.com/syndtr/goleveldb/leveldb"
  21. "github.com/syndtr/goleveldb/leveldb/storage"
  22. )
  23. var blocks = []protocol.BlockInfo{
  24. {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
  25. {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}},
  26. {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}},
  27. {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}},
  28. {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}},
  29. {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}},
  30. {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}},
  31. {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}},
  32. {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}},
  33. }
  34. // Layout of the files: (indexes from the above array)
  35. // 12345678 - Required file
  36. // 02005008 - Existing file (currently in the index)
  37. // 02340070 - Temp file on the disk
  38. func TestHandleFile(t *testing.T) {
  39. // After the diff between required and existing we should:
  40. // Copy: 2, 5, 8
  41. // Pull: 1, 3, 4, 6, 7
  42. // Create existing file, and update local index
  43. existingFile := protocol.FileInfo{
  44. Name: "filex",
  45. Flags: 0,
  46. Modified: 0,
  47. Blocks: []protocol.BlockInfo{
  48. blocks[0], blocks[2], blocks[0], blocks[0],
  49. blocks[5], blocks[0], blocks[0], blocks[8],
  50. },
  51. }
  52. // Create target file
  53. requiredFile := existingFile
  54. requiredFile.Blocks = blocks[1:]
  55. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  56. m := NewModel(config.Wrap("/tmp/test", config.Configuration{}), "device", "syncthing", "dev", db)
  57. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata"})
  58. m.updateLocal("default", existingFile)
  59. p := Puller{
  60. folder: "default",
  61. dir: "testdata",
  62. model: m,
  63. }
  64. copyChan := make(chan copyBlocksState, 1) // Copy chan gets all blocks needed to copy in a wrapper struct
  65. pullChan := make(chan pullBlockState, 5) // Pull chan gets blocks one by one
  66. p.handleFile(requiredFile, copyChan, pullChan, nil)
  67. // Receive the results
  68. toCopy := <-copyChan
  69. toPull := []pullBlockState{<-pullChan, <-pullChan, <-pullChan, <-pullChan, <-pullChan}
  70. select {
  71. case <-pullChan:
  72. t.Error("Channel not empty!")
  73. default:
  74. }
  75. if len(toCopy.blocks) != 3 {
  76. t.Errorf("Unexpected count of copy blocks: %d != 3", len(toCopy.blocks))
  77. }
  78. for i, eq := range []int{2, 5, 8} {
  79. if string(toCopy.blocks[i].Hash) != string(blocks[eq].Hash) {
  80. t.Errorf("Block mismatch: %s != %s", toCopy.blocks[i].String(), blocks[eq].String())
  81. }
  82. }
  83. for i, eq := range []int{1, 3, 4, 6, 7} {
  84. if string(toPull[i].block.Hash) != string(blocks[eq].Hash) {
  85. t.Errorf("Block mismatch: %s != %s", toPull[i].block.String(), blocks[eq].String())
  86. }
  87. }
  88. }
  89. func TestHandleFileWithTemp(t *testing.T) {
  90. // After diff between required and existing we should:
  91. // Copy: 2, 5, 8
  92. // Pull: 1, 3, 4, 6, 7
  93. // After dropping out blocks already on the temp file we should:
  94. // Copy: 5, 8
  95. // Pull: 1, 6
  96. // Create existing file, and update local index
  97. existingFile := protocol.FileInfo{
  98. Name: "file",
  99. Flags: 0,
  100. Modified: 0,
  101. Blocks: []protocol.BlockInfo{
  102. blocks[0], blocks[2], blocks[0], blocks[0],
  103. blocks[5], blocks[0], blocks[0], blocks[8],
  104. },
  105. }
  106. // Create target file
  107. requiredFile := existingFile
  108. requiredFile.Blocks = blocks[1:]
  109. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  110. m := NewModel(config.Wrap("/tmp/test", config.Configuration{}), "device", "syncthing", "dev", db)
  111. m.AddFolder(config.FolderConfiguration{ID: "default", Path: "testdata"})
  112. m.updateLocal("default", existingFile)
  113. p := Puller{
  114. folder: "default",
  115. dir: "testdata",
  116. model: m,
  117. }
  118. copyChan := make(chan copyBlocksState, 1) // Copy chan gets all blocks needed to copy in a wrapper struct
  119. pullChan := make(chan pullBlockState, 2) // Pull chan gets blocks one by one
  120. p.handleFile(requiredFile, copyChan, pullChan, nil)
  121. // Receive the results
  122. toCopy := <-copyChan
  123. toPull := []pullBlockState{<-pullChan, <-pullChan}
  124. select {
  125. case <-pullChan:
  126. t.Error("Channel not empty!")
  127. default:
  128. }
  129. if len(toCopy.blocks) != 2 {
  130. t.Errorf("Unexpected count of copy blocks: %d != 2", len(toCopy.blocks))
  131. }
  132. for i, eq := range []int{5, 8} {
  133. if string(toCopy.blocks[i].Hash) != string(blocks[eq].Hash) {
  134. t.Errorf("Block mismatch: %s != %s", toCopy.blocks[i].String(), blocks[eq].String())
  135. }
  136. }
  137. for i, eq := range []int{1, 6} {
  138. if string(toPull[i].block.Hash) != string(blocks[eq].Hash) {
  139. t.Errorf("Block mismatch: %s != %s", toPull[i].block.String(), blocks[eq].String())
  140. }
  141. }
  142. }