walk_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. "github.com/syncthing/syncthing/internal/osutil"
  20. "golang.org/x/text/unicode/norm"
  21. )
  22. type testfile struct {
  23. name string
  24. size int
  25. hash string
  26. }
  27. type testfileList []testfile
  28. var testdata = testfileList{
  29. {"afile", 4, "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"},
  30. {"dir1", 128, ""},
  31. {filepath.Join("dir1", "dfile"), 5, "49ae93732fcf8d63fe1cce759664982dbd5b23161f007dba8561862adc96d063"},
  32. {"dir2", 128, ""},
  33. {filepath.Join("dir2", "cfile"), 4, "bf07a7fbb825fc0aae7bf4a1177b2b31fcf8a3feeaf7092761e18c859ee52a9c"},
  34. {"excludes", 37, "df90b52f0c55dba7a7a940affe482571563b1ac57bd5be4d8a0291e7de928e06"},
  35. {"further-excludes", 5, "7eb0a548094fa6295f7fd9200d69973e5f5ec5c04f2a86d998080ac43ecf89f1"},
  36. }
  37. var correctIgnores = map[string][]string{
  38. ".": {".*", "quux"},
  39. }
  40. func init() {
  41. // This test runs the risk of entering infinite recursion if it fails.
  42. // Limit the stack size to 10 megs to crash early in that case instead of
  43. // potentially taking down the box...
  44. rdebug.SetMaxStack(10 * 1 << 20)
  45. }
  46. func TestWalkSub(t *testing.T) {
  47. ignores := ignore.New(false)
  48. err := ignores.Load("testdata/.stignore")
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. w := Walker{
  53. Dir: "testdata",
  54. Subs: []string{"dir2"},
  55. BlockSize: 128 * 1024,
  56. Matcher: ignores,
  57. Hashers: 2,
  58. }
  59. fchan, err := w.Walk()
  60. var files []protocol.FileInfo
  61. for f := range fchan {
  62. files = append(files, f)
  63. }
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. // The directory contains two files, where one is ignored from a higher
  68. // level. We should see only the directory and one of the files.
  69. if len(files) != 2 {
  70. t.Fatalf("Incorrect length %d != 2", len(files))
  71. }
  72. if files[0].Name != "dir2" {
  73. t.Errorf("Incorrect file %v != dir2", files[0])
  74. }
  75. if files[1].Name != filepath.Join("dir2", "cfile") {
  76. t.Errorf("Incorrect file %v != dir2/cfile", files[1])
  77. }
  78. }
  79. func TestWalk(t *testing.T) {
  80. ignores := ignore.New(false)
  81. err := ignores.Load("testdata/.stignore")
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. t.Log(ignores)
  86. w := Walker{
  87. Dir: "testdata",
  88. BlockSize: 128 * 1024,
  89. Matcher: ignores,
  90. Hashers: 2,
  91. }
  92. fchan, err := w.Walk()
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. var tmp []protocol.FileInfo
  97. for f := range fchan {
  98. tmp = append(tmp, f)
  99. }
  100. sort.Sort(fileList(tmp))
  101. files := fileList(tmp).testfiles()
  102. if !reflect.DeepEqual(files, testdata) {
  103. t.Errorf("Walk returned unexpected data\nExpected: %v\nActual: %v", testdata, files)
  104. }
  105. }
  106. func TestWalkError(t *testing.T) {
  107. w := Walker{
  108. Dir: "testdata-missing",
  109. BlockSize: 128 * 1024,
  110. Hashers: 2,
  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. func TestNormalization(t *testing.T) {
  164. if runtime.GOOS == "darwin" {
  165. t.Skip("Normalization test not possible on darwin")
  166. return
  167. }
  168. os.RemoveAll("testdata/normalization")
  169. defer os.RemoveAll("testdata/normalization")
  170. tests := []string{
  171. "0-A", // ASCII A -- accepted
  172. "1-\xC3\x84", // NFC 'Ä' -- conflicts with the entry below, accepted
  173. "1-\x41\xCC\x88", // NFD 'Ä' -- conflicts with the entry above, ignored
  174. "2-\xC3\x85", // NFC 'Å' -- accepted
  175. "3-\x41\xCC\x83", // NFD 'Ã' -- converted to NFC
  176. "4-\xE2\x98\x95", // U+2615 HOT BEVERAGE (☕) -- accepted
  177. "5-\xCD\xE2", // EUC-CN "wài" (外) -- ignored (not UTF8)
  178. }
  179. numInvalid := 2
  180. if runtime.GOOS == "windows" {
  181. // On Windows, in case 5 the character gets replaced with a
  182. // replacement character \xEF\xBF\xBD at the point it's written to disk,
  183. // which means it suddenly becomes valid (sort of).
  184. numInvalid--
  185. }
  186. numValid := len(tests) - numInvalid
  187. for _, s1 := range tests {
  188. // Create a directory for each of the interesting strings above
  189. if err := osutil.MkdirAll(filepath.Join("testdata/normalization", s1), 0755); err != nil {
  190. t.Fatal(err)
  191. }
  192. for _, s2 := range tests {
  193. // Within each dir, create a file with each of the interesting
  194. // file names. Ensure that the file doesn't exist when it's
  195. // created. This detects and fails if there's file name
  196. // normalization stuff at the filesystem level.
  197. if fd, err := os.OpenFile(filepath.Join("testdata/normalization", s1, s2), os.O_CREATE|os.O_EXCL, 0644); err != nil {
  198. t.Fatal(err)
  199. } else {
  200. fd.WriteString("test")
  201. fd.Close()
  202. }
  203. }
  204. }
  205. // We can normalize a directory name, but we can't descend into it in the
  206. // same pass due to how filepath.Walk works. So we run the scan twice to
  207. // make sure it all gets done. In production, things will be correct
  208. // eventually...
  209. _, err := walkDir("testdata/normalization")
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. tmp, err := walkDir("testdata/normalization")
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. files := fileList(tmp).testfiles()
  218. // We should have one file per combination, plus the directories
  219. // themselves
  220. expectedNum := numValid*numValid + numValid
  221. if len(files) != expectedNum {
  222. t.Errorf("Expected %d files, got %d", expectedNum, len(files))
  223. }
  224. // The file names should all be in NFC form.
  225. for _, f := range files {
  226. t.Logf("%q (% x) %v", f.name, f.name, norm.NFC.IsNormalString(f.name))
  227. if !norm.NFC.IsNormalString(f.name) {
  228. t.Errorf("File name %q is not NFC normalized", f.name)
  229. }
  230. }
  231. }
  232. func TestIssue1507(t *testing.T) {
  233. w := Walker{}
  234. c := make(chan protocol.FileInfo, 100)
  235. fn := w.walkAndHashFiles(c)
  236. fn("", nil, protocol.ErrClosed)
  237. }
  238. func walkDir(dir string) ([]protocol.FileInfo, error) {
  239. w := Walker{
  240. Dir: dir,
  241. BlockSize: 128 * 1024,
  242. AutoNormalize: true,
  243. Hashers: 2,
  244. }
  245. fchan, err := w.Walk()
  246. if err != nil {
  247. return nil, err
  248. }
  249. var tmp []protocol.FileInfo
  250. for f := range fchan {
  251. tmp = append(tmp, f)
  252. }
  253. sort.Sort(fileList(tmp))
  254. return tmp, nil
  255. }
  256. type fileList []protocol.FileInfo
  257. func (l fileList) Len() int {
  258. return len(l)
  259. }
  260. func (l fileList) Less(a, b int) bool {
  261. return l[a].Name < l[b].Name
  262. }
  263. func (l fileList) Swap(a, b int) {
  264. l[a], l[b] = l[b], l[a]
  265. }
  266. func (l fileList) testfiles() testfileList {
  267. testfiles := make(testfileList, len(l))
  268. for i, f := range l {
  269. if len(f.Blocks) > 1 {
  270. panic("simple test case stuff only supports a single block per file")
  271. }
  272. testfiles[i] = testfile{name: f.Name, size: int(f.Size())}
  273. if len(f.Blocks) == 1 {
  274. testfiles[i].hash = fmt.Sprintf("%x", f.Blocks[0].Hash)
  275. }
  276. }
  277. return testfiles
  278. }
  279. func (l testfileList) String() string {
  280. var b bytes.Buffer
  281. b.WriteString("{\n")
  282. for _, f := range l {
  283. fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.size, f.hash)
  284. }
  285. b.WriteString("}")
  286. return b.String()
  287. }