walk_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 scanner
  16. import (
  17. "bytes"
  18. "fmt"
  19. "path/filepath"
  20. "reflect"
  21. rdebug "runtime/debug"
  22. "sort"
  23. "testing"
  24. "github.com/syncthing/syncthing/internal/ignore"
  25. "github.com/syncthing/syncthing/internal/protocol"
  26. )
  27. type testfile struct {
  28. name string
  29. size int
  30. hash string
  31. }
  32. type testfileList []testfile
  33. var testdata = testfileList{
  34. {"afile", 4, "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"},
  35. {"dir1", 128, ""},
  36. {filepath.Join("dir1", "dfile"), 5, "49ae93732fcf8d63fe1cce759664982dbd5b23161f007dba8561862adc96d063"},
  37. {"dir2", 128, ""},
  38. {filepath.Join("dir2", "cfile"), 4, "bf07a7fbb825fc0aae7bf4a1177b2b31fcf8a3feeaf7092761e18c859ee52a9c"},
  39. {"excludes", 37, "df90b52f0c55dba7a7a940affe482571563b1ac57bd5be4d8a0291e7de928e06"},
  40. {"further-excludes", 5, "7eb0a548094fa6295f7fd9200d69973e5f5ec5c04f2a86d998080ac43ecf89f1"},
  41. }
  42. var correctIgnores = map[string][]string{
  43. ".": {".*", "quux"},
  44. }
  45. func init() {
  46. // This test runs the risk of entering infinite recursion if it fails.
  47. // Limit the stack size to 10 megs to creash early in that case instead of
  48. // potentially taking down the box...
  49. rdebug.SetMaxStack(10 * 1 << 20)
  50. }
  51. func TestWalkSub(t *testing.T) {
  52. ignores := ignore.New(false)
  53. err := ignores.Load("testdata/.stignore")
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. w := Walker{
  58. Dir: "testdata",
  59. Sub: "dir2",
  60. BlockSize: 128 * 1024,
  61. Matcher: ignores,
  62. }
  63. fchan, err := w.Walk()
  64. var files []protocol.FileInfo
  65. for f := range fchan {
  66. files = append(files, f)
  67. }
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. // The directory contains two files, where one is ignored from a higher
  72. // level. We should see only the directory and one of the files.
  73. if len(files) != 2 {
  74. t.Fatalf("Incorrect length %d != 2", len(files))
  75. }
  76. if files[0].Name != "dir2" {
  77. t.Errorf("Incorrect file %v != dir2", files[0])
  78. }
  79. if files[1].Name != filepath.Join("dir2", "cfile") {
  80. t.Errorf("Incorrect file %v != dir2/cfile", files[1])
  81. }
  82. }
  83. func TestWalk(t *testing.T) {
  84. ignores := ignore.New(false)
  85. err := ignores.Load("testdata/.stignore")
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. t.Log(ignores)
  90. w := Walker{
  91. Dir: "testdata",
  92. BlockSize: 128 * 1024,
  93. Matcher: ignores,
  94. }
  95. fchan, err := w.Walk()
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. var tmp []protocol.FileInfo
  100. for f := range fchan {
  101. tmp = append(tmp, f)
  102. }
  103. sort.Sort(fileList(tmp))
  104. files := fileList(tmp).testfiles()
  105. if !reflect.DeepEqual(files, testdata) {
  106. t.Errorf("Walk returned unexpected data\nExpected: %v\nActual: %v", testdata, files)
  107. }
  108. }
  109. func TestWalkError(t *testing.T) {
  110. w := Walker{
  111. Dir: "testdata-missing",
  112. BlockSize: 128 * 1024,
  113. }
  114. _, err := w.Walk()
  115. if err == nil {
  116. t.Error("no error from missing directory")
  117. }
  118. w = Walker{
  119. Dir: "testdata/bar",
  120. BlockSize: 128 * 1024,
  121. }
  122. _, err = w.Walk()
  123. if err == nil {
  124. t.Error("no error from non-directory")
  125. }
  126. }
  127. func TestVerify(t *testing.T) {
  128. blocksize := 16
  129. // data should be an even multiple of blocksize long
  130. data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e")
  131. buf := bytes.NewBuffer(data)
  132. blocks, err := Blocks(buf, blocksize, 0)
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. if exp := len(data) / blocksize; len(blocks) != exp {
  137. t.Fatalf("Incorrect number of blocks %d != %d", len(blocks), exp)
  138. }
  139. buf = bytes.NewBuffer(data)
  140. err = Verify(buf, blocksize, blocks)
  141. t.Log(err)
  142. if err != nil {
  143. t.Fatal("Unexpected verify failure", err)
  144. }
  145. buf = bytes.NewBuffer(append(data, '\n'))
  146. err = Verify(buf, blocksize, blocks)
  147. t.Log(err)
  148. if err == nil {
  149. t.Fatal("Unexpected verify success")
  150. }
  151. buf = bytes.NewBuffer(data[:len(data)-1])
  152. err = Verify(buf, blocksize, blocks)
  153. t.Log(err)
  154. if err == nil {
  155. t.Fatal("Unexpected verify success")
  156. }
  157. data[42] = 42
  158. buf = bytes.NewBuffer(data)
  159. err = Verify(buf, blocksize, blocks)
  160. t.Log(err)
  161. if err == nil {
  162. t.Fatal("Unexpected verify success")
  163. }
  164. }
  165. type fileList []protocol.FileInfo
  166. func (l fileList) Len() int {
  167. return len(l)
  168. }
  169. func (l fileList) Less(a, b int) bool {
  170. return l[a].Name < l[b].Name
  171. }
  172. func (l fileList) Swap(a, b int) {
  173. l[a], l[b] = l[b], l[a]
  174. }
  175. func (l fileList) testfiles() testfileList {
  176. testfiles := make(testfileList, len(l))
  177. for i, f := range l {
  178. if len(f.Blocks) > 1 {
  179. panic("simple test case stuff only supports a single block per file")
  180. }
  181. testfiles[i] = testfile{name: f.Name, size: int(f.Size())}
  182. if len(f.Blocks) == 1 {
  183. testfiles[i].hash = fmt.Sprintf("%x", f.Blocks[0].Hash)
  184. }
  185. }
  186. return testfiles
  187. }
  188. func (l testfileList) String() string {
  189. var b bytes.Buffer
  190. b.WriteString("{\n")
  191. for _, f := range l {
  192. fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.size, f.hash)
  193. }
  194. b.WriteString("}")
  195. return b.String()
  196. }