walk_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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, err := ignore.Load("testdata/.stignore", false)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. w := Walker{
  57. Dir: "testdata",
  58. Sub: "dir2",
  59. BlockSize: 128 * 1024,
  60. Matcher: ignores,
  61. }
  62. fchan, err := w.Walk()
  63. var files []protocol.FileInfo
  64. for f := range fchan {
  65. files = append(files, f)
  66. }
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. // The directory contains two files, where one is ignored from a higher
  71. // level. We should see only the directory and one of the files.
  72. if len(files) != 2 {
  73. t.Fatalf("Incorrect length %d != 2", len(files))
  74. }
  75. if files[0].Name != "dir2" {
  76. t.Errorf("Incorrect file %v != dir2", files[0])
  77. }
  78. if files[1].Name != filepath.Join("dir2", "cfile") {
  79. t.Errorf("Incorrect file %v != dir2/cfile", files[1])
  80. }
  81. }
  82. func TestWalk(t *testing.T) {
  83. ignores, err := ignore.Load("testdata/.stignore", false)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. t.Log(ignores)
  88. w := Walker{
  89. Dir: "testdata",
  90. BlockSize: 128 * 1024,
  91. Matcher: ignores,
  92. }
  93. fchan, err := w.Walk()
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. var tmp []protocol.FileInfo
  98. for f := range fchan {
  99. tmp = append(tmp, f)
  100. }
  101. sort.Sort(fileList(tmp))
  102. files := fileList(tmp).testfiles()
  103. if !reflect.DeepEqual(files, testdata) {
  104. t.Errorf("Walk returned unexpected data\nExpected: %v\nActual: %v", testdata, files)
  105. }
  106. }
  107. func TestWalkError(t *testing.T) {
  108. w := Walker{
  109. Dir: "testdata-missing",
  110. BlockSize: 128 * 1024,
  111. }
  112. _, err := w.Walk()
  113. if err == nil {
  114. t.Error("no error from missing directory")
  115. }
  116. w = Walker{
  117. Dir: "testdata/bar",
  118. BlockSize: 128 * 1024,
  119. }
  120. _, err = w.Walk()
  121. if err == nil {
  122. t.Error("no error from non-directory")
  123. }
  124. }
  125. func TestVerify(t *testing.T) {
  126. blocksize := 16
  127. // data should be an even multiple of blocksize long
  128. data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e")
  129. buf := bytes.NewBuffer(data)
  130. blocks, err := Blocks(buf, blocksize, 0)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. if exp := len(data) / blocksize; len(blocks) != exp {
  135. t.Fatalf("Incorrect number of blocks %d != %d", len(blocks), exp)
  136. }
  137. buf = bytes.NewBuffer(data)
  138. err = Verify(buf, blocksize, blocks)
  139. t.Log(err)
  140. if err != nil {
  141. t.Fatal("Unexpected verify failure", err)
  142. }
  143. buf = bytes.NewBuffer(append(data, '\n'))
  144. err = Verify(buf, blocksize, blocks)
  145. t.Log(err)
  146. if err == nil {
  147. t.Fatal("Unexpected verify success")
  148. }
  149. buf = bytes.NewBuffer(data[:len(data)-1])
  150. err = Verify(buf, blocksize, blocks)
  151. t.Log(err)
  152. if err == nil {
  153. t.Fatal("Unexpected verify success")
  154. }
  155. data[42] = 42
  156. buf = bytes.NewBuffer(data)
  157. err = Verify(buf, blocksize, blocks)
  158. t.Log(err)
  159. if err == nil {
  160. t.Fatal("Unexpected verify success")
  161. }
  162. }
  163. type fileList []protocol.FileInfo
  164. func (l fileList) Len() int {
  165. return len(l)
  166. }
  167. func (l fileList) Less(a, b int) bool {
  168. return l[a].Name < l[b].Name
  169. }
  170. func (l fileList) Swap(a, b int) {
  171. l[a], l[b] = l[b], l[a]
  172. }
  173. func (l fileList) testfiles() testfileList {
  174. testfiles := make(testfileList, len(l))
  175. for i, f := range l {
  176. if len(f.Blocks) > 1 {
  177. panic("simple test case stuff only supports a single block per file")
  178. }
  179. testfiles[i] = testfile{name: f.Name, size: int(f.Size())}
  180. if len(f.Blocks) == 1 {
  181. testfiles[i].hash = fmt.Sprintf("%x", f.Blocks[0].Hash)
  182. }
  183. }
  184. return testfiles
  185. }
  186. func (l testfileList) String() string {
  187. var b bytes.Buffer
  188. b.WriteString("{\n")
  189. for _, f := range l {
  190. fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.size, f.hash)
  191. }
  192. b.WriteString("}")
  193. return b.String()
  194. }