walk_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package scanner
  7. import (
  8. "bytes"
  9. "fmt"
  10. "os"
  11. "path/filepath"
  12. "reflect"
  13. "runtime"
  14. rdebug "runtime/debug"
  15. "sort"
  16. "testing"
  17. "github.com/syncthing/protocol"
  18. "github.com/syncthing/syncthing/internal/ignore"
  19. "golang.org/x/text/unicode/norm"
  20. )
  21. type testfile struct {
  22. name string
  23. size int
  24. hash string
  25. }
  26. type testfileList []testfile
  27. var testdata = testfileList{
  28. {"afile", 4, "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"},
  29. {"dir1", 128, ""},
  30. {filepath.Join("dir1", "dfile"), 5, "49ae93732fcf8d63fe1cce759664982dbd5b23161f007dba8561862adc96d063"},
  31. {"dir2", 128, ""},
  32. {filepath.Join("dir2", "cfile"), 4, "bf07a7fbb825fc0aae7bf4a1177b2b31fcf8a3feeaf7092761e18c859ee52a9c"},
  33. {"excludes", 37, "df90b52f0c55dba7a7a940affe482571563b1ac57bd5be4d8a0291e7de928e06"},
  34. {"further-excludes", 5, "7eb0a548094fa6295f7fd9200d69973e5f5ec5c04f2a86d998080ac43ecf89f1"},
  35. }
  36. var correctIgnores = map[string][]string{
  37. ".": {".*", "quux"},
  38. }
  39. func init() {
  40. // This test runs the risk of entering infinite recursion if it fails.
  41. // Limit the stack size to 10 megs to creash early in that case instead of
  42. // potentially taking down the box...
  43. rdebug.SetMaxStack(10 * 1 << 20)
  44. }
  45. func TestWalkSub(t *testing.T) {
  46. ignores := ignore.New(false)
  47. err := ignores.Load("testdata/.stignore")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. w := Walker{
  52. Dir: "testdata",
  53. Subs: []string{"dir2"},
  54. BlockSize: 128 * 1024,
  55. Matcher: ignores,
  56. }
  57. fchan, err := w.Walk()
  58. var files []protocol.FileInfo
  59. for f := range fchan {
  60. files = append(files, f)
  61. }
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. // The directory contains two files, where one is ignored from a higher
  66. // level. We should see only the directory and one of the files.
  67. if len(files) != 2 {
  68. t.Fatalf("Incorrect length %d != 2", len(files))
  69. }
  70. if files[0].Name != "dir2" {
  71. t.Errorf("Incorrect file %v != dir2", files[0])
  72. }
  73. if files[1].Name != filepath.Join("dir2", "cfile") {
  74. t.Errorf("Incorrect file %v != dir2/cfile", files[1])
  75. }
  76. }
  77. func TestWalk(t *testing.T) {
  78. ignores := ignore.New(false)
  79. err := ignores.Load("testdata/.stignore")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. t.Log(ignores)
  84. w := Walker{
  85. Dir: "testdata",
  86. BlockSize: 128 * 1024,
  87. Matcher: ignores,
  88. }
  89. fchan, err := w.Walk()
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. var tmp []protocol.FileInfo
  94. for f := range fchan {
  95. tmp = append(tmp, f)
  96. }
  97. sort.Sort(fileList(tmp))
  98. files := fileList(tmp).testfiles()
  99. if !reflect.DeepEqual(files, testdata) {
  100. t.Errorf("Walk returned unexpected data\nExpected: %v\nActual: %v", testdata, files)
  101. }
  102. }
  103. func TestWalkError(t *testing.T) {
  104. w := Walker{
  105. Dir: "testdata-missing",
  106. BlockSize: 128 * 1024,
  107. }
  108. _, err := w.Walk()
  109. if err == nil {
  110. t.Error("no error from missing directory")
  111. }
  112. w = Walker{
  113. Dir: "testdata/bar",
  114. BlockSize: 128 * 1024,
  115. }
  116. _, err = w.Walk()
  117. if err == nil {
  118. t.Error("no error from non-directory")
  119. }
  120. }
  121. func TestVerify(t *testing.T) {
  122. blocksize := 16
  123. // data should be an even multiple of blocksize long
  124. data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e")
  125. buf := bytes.NewBuffer(data)
  126. blocks, err := Blocks(buf, blocksize, 0)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. if exp := len(data) / blocksize; len(blocks) != exp {
  131. t.Fatalf("Incorrect number of blocks %d != %d", len(blocks), exp)
  132. }
  133. buf = bytes.NewBuffer(data)
  134. err = Verify(buf, blocksize, blocks)
  135. t.Log(err)
  136. if err != nil {
  137. t.Fatal("Unexpected verify failure", err)
  138. }
  139. buf = bytes.NewBuffer(append(data, '\n'))
  140. err = Verify(buf, blocksize, blocks)
  141. t.Log(err)
  142. if err == nil {
  143. t.Fatal("Unexpected verify success")
  144. }
  145. buf = bytes.NewBuffer(data[:len(data)-1])
  146. err = Verify(buf, blocksize, blocks)
  147. t.Log(err)
  148. if err == nil {
  149. t.Fatal("Unexpected verify success")
  150. }
  151. data[42] = 42
  152. buf = bytes.NewBuffer(data)
  153. err = Verify(buf, blocksize, blocks)
  154. t.Log(err)
  155. if err == nil {
  156. t.Fatal("Unexpected verify success")
  157. }
  158. }
  159. func TestNormalization(t *testing.T) {
  160. if runtime.GOOS == "darwin" {
  161. t.Skip("Normalization test not possible on darwin")
  162. return
  163. }
  164. os.RemoveAll("testdata/normalization")
  165. defer os.RemoveAll("testdata/normalization")
  166. tests := []string{
  167. "0-A", // ASCII A -- accepted
  168. "1-\xC3\x84", // NFC 'Ä' -- conflicts with the entry below, accepted
  169. "1-\x41\xCC\x88", // NFD 'Ä' -- conflicts with the entry above, ignored
  170. "2-\xC3\x85", // NFC 'Å' -- accepted
  171. "3-\x41\xCC\x83", // NFD 'Ã' -- converted to NFC
  172. "4-\xE2\x98\x95", // U+2615 HOT BEVERAGE (☕) -- accepted
  173. "5-\xCD\xE2", // EUC-CN "wài" (外) -- ignored (not UTF8)
  174. }
  175. numInvalid := 2
  176. numValid := len(tests) - numInvalid
  177. for _, s1 := range tests {
  178. // Create a directory for each of the interesting strings above
  179. if err := os.MkdirAll(filepath.Join("testdata/normalization", s1), 0755); err != nil {
  180. t.Fatal(err)
  181. }
  182. for _, s2 := range tests {
  183. // Within each dir, create a file with each of the interesting
  184. // file names. Ensure that the file doesn't exist when it's
  185. // created. This detects and fails if there's file name
  186. // normalization stuff at the filesystem level.
  187. if fd, err := os.OpenFile(filepath.Join("testdata/normalization", s1, s2), os.O_CREATE|os.O_EXCL, 0644); err != nil {
  188. t.Fatal(err)
  189. } else {
  190. fd.WriteString("test")
  191. fd.Close()
  192. }
  193. }
  194. }
  195. // We can normalize a directory name, but we can't descend into it in the
  196. // same pass due to how filepath.Walk works. So we run the scan twice to
  197. // make sure it all gets done. In production, things will be correct
  198. // eventually...
  199. _, err := walkDir("testdata/normalization")
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. tmp, err := walkDir("testdata/normalization")
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. files := fileList(tmp).testfiles()
  208. // We should have one file per combination, plus the directories
  209. // themselves
  210. expectedNum := numValid*numValid + numValid
  211. if len(files) != expectedNum {
  212. t.Errorf("Expected %d files, got %d", expectedNum, len(files))
  213. }
  214. // The file names should all be in NFC form.
  215. for _, f := range files {
  216. t.Logf("%q (% x) %v", f.name, f.name, norm.NFC.IsNormalString(f.name))
  217. if !norm.NFC.IsNormalString(f.name) {
  218. t.Errorf("File name %q is not NFC normalized", f.name)
  219. }
  220. }
  221. }
  222. func TestIssue1507(t *testing.T) {
  223. w := Walker{}
  224. c := make(chan protocol.FileInfo, 100)
  225. fn := w.walkAndHashFiles(c)
  226. fn("", nil, protocol.ErrClosed)
  227. }
  228. func walkDir(dir string) ([]protocol.FileInfo, error) {
  229. w := Walker{
  230. Dir: dir,
  231. BlockSize: 128 * 1024,
  232. AutoNormalize: true,
  233. }
  234. fchan, err := w.Walk()
  235. if err != nil {
  236. return nil, err
  237. }
  238. var tmp []protocol.FileInfo
  239. for f := range fchan {
  240. tmp = append(tmp, f)
  241. }
  242. sort.Sort(fileList(tmp))
  243. return tmp, nil
  244. }
  245. type fileList []protocol.FileInfo
  246. func (l fileList) Len() int {
  247. return len(l)
  248. }
  249. func (l fileList) Less(a, b int) bool {
  250. return l[a].Name < l[b].Name
  251. }
  252. func (l fileList) Swap(a, b int) {
  253. l[a], l[b] = l[b], l[a]
  254. }
  255. func (l fileList) testfiles() testfileList {
  256. testfiles := make(testfileList, len(l))
  257. for i, f := range l {
  258. if len(f.Blocks) > 1 {
  259. panic("simple test case stuff only supports a single block per file")
  260. }
  261. testfiles[i] = testfile{name: f.Name, size: int(f.Size())}
  262. if len(f.Blocks) == 1 {
  263. testfiles[i].hash = fmt.Sprintf("%x", f.Blocks[0].Hash)
  264. }
  265. }
  266. return testfiles
  267. }
  268. func (l testfileList) String() string {
  269. var b bytes.Buffer
  270. b.WriteString("{\n")
  271. for _, f := range l {
  272. fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.size, f.hash)
  273. }
  274. b.WriteString("}")
  275. return b.String()
  276. }