walk_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. "crypto/rand"
  10. "fmt"
  11. "io"
  12. "os"
  13. "path/filepath"
  14. "runtime"
  15. rdebug "runtime/debug"
  16. "sort"
  17. "sync"
  18. "testing"
  19. "github.com/d4l3k/messagediff"
  20. "github.com/syncthing/syncthing/lib/ignore"
  21. "github.com/syncthing/syncthing/lib/osutil"
  22. "github.com/syncthing/syncthing/lib/protocol"
  23. "golang.org/x/text/unicode/norm"
  24. )
  25. type testfile struct {
  26. name string
  27. length int64
  28. hash string
  29. }
  30. type testfileList []testfile
  31. var testdata = testfileList{
  32. {"afile", 4, "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"},
  33. {"dir1", 128, ""},
  34. {filepath.Join("dir1", "dfile"), 5, "49ae93732fcf8d63fe1cce759664982dbd5b23161f007dba8561862adc96d063"},
  35. {"dir2", 128, ""},
  36. {filepath.Join("dir2", "cfile"), 4, "bf07a7fbb825fc0aae7bf4a1177b2b31fcf8a3feeaf7092761e18c859ee52a9c"},
  37. {"excludes", 37, "df90b52f0c55dba7a7a940affe482571563b1ac57bd5be4d8a0291e7de928e06"},
  38. {"further-excludes", 5, "7eb0a548094fa6295f7fd9200d69973e5f5ec5c04f2a86d998080ac43ecf89f1"},
  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. fchan, err := Walk(Config{
  53. Dir: "testdata",
  54. Subs: []string{"dir2"},
  55. BlockSize: 128 * 1024,
  56. Matcher: ignores,
  57. Hashers: 2,
  58. })
  59. var files []protocol.FileInfo
  60. for f := range fchan {
  61. files = append(files, f)
  62. }
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. // The directory contains two files, where one is ignored from a higher
  67. // level. We should see only the directory and one of the files.
  68. if len(files) != 2 {
  69. t.Fatalf("Incorrect length %d != 2", len(files))
  70. }
  71. if files[0].Name != "dir2" {
  72. t.Errorf("Incorrect file %v != dir2", files[0])
  73. }
  74. if files[1].Name != filepath.Join("dir2", "cfile") {
  75. t.Errorf("Incorrect file %v != dir2/cfile", files[1])
  76. }
  77. }
  78. func TestWalk(t *testing.T) {
  79. ignores := ignore.New(false)
  80. err := ignores.Load("testdata/.stignore")
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. t.Log(ignores)
  85. fchan, err := Walk(Config{
  86. Dir: "testdata",
  87. BlockSize: 128 * 1024,
  88. Matcher: ignores,
  89. Hashers: 2,
  90. })
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. var tmp []protocol.FileInfo
  95. for f := range fchan {
  96. tmp = append(tmp, f)
  97. }
  98. sort.Sort(fileList(tmp))
  99. files := fileList(tmp).testfiles()
  100. if diff, equal := messagediff.PrettyDiff(testdata, files); !equal {
  101. t.Errorf("Walk returned unexpected data. Diff:\n%s", diff)
  102. }
  103. }
  104. func TestWalkError(t *testing.T) {
  105. _, err := Walk(Config{
  106. Dir: "testdata-missing",
  107. BlockSize: 128 * 1024,
  108. Hashers: 2,
  109. })
  110. if err == nil {
  111. t.Error("no error from missing directory")
  112. }
  113. _, err = Walk(Config{
  114. Dir: "testdata/bar",
  115. BlockSize: 128 * 1024,
  116. })
  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. progress := newByteCounter()
  127. defer progress.Close()
  128. blocks, err := Blocks(buf, blocksize, -1, progress, false)
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. if exp := len(data) / blocksize; len(blocks) != exp {
  133. t.Fatalf("Incorrect number of blocks %d != %d", len(blocks), exp)
  134. }
  135. if int64(len(data)) != progress.Total() {
  136. t.Fatalf("Incorrect counter value %d != %d", len(data), progress.Total())
  137. }
  138. buf = bytes.NewBuffer(data)
  139. err = Verify(buf, blocksize, blocks)
  140. t.Log(err)
  141. if err != nil {
  142. t.Fatal("Unexpected verify failure", err)
  143. }
  144. buf = bytes.NewBuffer(append(data, '\n'))
  145. err = Verify(buf, blocksize, blocks)
  146. t.Log(err)
  147. if err == nil {
  148. t.Fatal("Unexpected verify success")
  149. }
  150. buf = bytes.NewBuffer(data[:len(data)-1])
  151. err = Verify(buf, blocksize, blocks)
  152. t.Log(err)
  153. if err == nil {
  154. t.Fatal("Unexpected verify success")
  155. }
  156. data[42] = 42
  157. buf = bytes.NewBuffer(data)
  158. err = Verify(buf, blocksize, blocks)
  159. t.Log(err)
  160. if err == nil {
  161. t.Fatal("Unexpected verify success")
  162. }
  163. }
  164. func TestNormalization(t *testing.T) {
  165. if runtime.GOOS == "darwin" {
  166. t.Skip("Normalization test not possible on darwin")
  167. return
  168. }
  169. os.RemoveAll("testdata/normalization")
  170. defer os.RemoveAll("testdata/normalization")
  171. tests := []string{
  172. "0-A", // ASCII A -- accepted
  173. "1-\xC3\x84", // NFC 'Ä' -- conflicts with the entry below, accepted
  174. "1-\x41\xCC\x88", // NFD 'Ä' -- conflicts with the entry above, ignored
  175. "2-\xC3\x85", // NFC 'Å' -- accepted
  176. "3-\x41\xCC\x83", // NFD 'Ã' -- converted to NFC
  177. "4-\xE2\x98\x95", // U+2615 HOT BEVERAGE (☕) -- accepted
  178. "5-\xCD\xE2", // EUC-CN "wài" (外) -- ignored (not UTF8)
  179. }
  180. numInvalid := 2
  181. if runtime.GOOS == "windows" {
  182. // On Windows, in case 5 the character gets replaced with a
  183. // replacement character \xEF\xBF\xBD at the point it's written to disk,
  184. // which means it suddenly becomes valid (sort of).
  185. numInvalid--
  186. }
  187. numValid := len(tests) - numInvalid
  188. for _, s1 := range tests {
  189. // Create a directory for each of the interesting strings above
  190. if err := osutil.MkdirAll(filepath.Join("testdata/normalization", s1), 0755); err != nil {
  191. t.Fatal(err)
  192. }
  193. for _, s2 := range tests {
  194. // Within each dir, create a file with each of the interesting
  195. // file names. Ensure that the file doesn't exist when it's
  196. // created. This detects and fails if there's file name
  197. // normalization stuff at the filesystem level.
  198. if fd, err := os.OpenFile(filepath.Join("testdata/normalization", s1, s2), os.O_CREATE|os.O_EXCL, 0644); err != nil {
  199. t.Fatal(err)
  200. } else {
  201. fd.WriteString("test")
  202. fd.Close()
  203. }
  204. }
  205. }
  206. // We can normalize a directory name, but we can't descend into it in the
  207. // same pass due to how filepath.Walk works. So we run the scan twice to
  208. // make sure it all gets done. In production, things will be correct
  209. // eventually...
  210. _, err := walkDir("testdata/normalization")
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. tmp, err := walkDir("testdata/normalization")
  215. if err != nil {
  216. t.Fatal(err)
  217. }
  218. files := fileList(tmp).testfiles()
  219. // We should have one file per combination, plus the directories
  220. // themselves
  221. expectedNum := numValid*numValid + numValid
  222. if len(files) != expectedNum {
  223. t.Errorf("Expected %d files, got %d", expectedNum, len(files))
  224. }
  225. // The file names should all be in NFC form.
  226. for _, f := range files {
  227. t.Logf("%q (% x) %v", f.name, f.name, norm.NFC.IsNormalString(f.name))
  228. if !norm.NFC.IsNormalString(f.name) {
  229. t.Errorf("File name %q is not NFC normalized", f.name)
  230. }
  231. }
  232. }
  233. func TestIssue1507(t *testing.T) {
  234. w := &walker{}
  235. c := make(chan protocol.FileInfo, 100)
  236. fn := w.walkAndHashFiles(c, c)
  237. fn("", nil, protocol.ErrClosed)
  238. }
  239. func TestWalkSymlink(t *testing.T) {
  240. if runtime.GOOS == "windows" {
  241. t.Skip("skipping unsupported symlink test")
  242. return
  243. }
  244. // Create a folder with a symlink in it
  245. os.RemoveAll("_symlinks")
  246. defer os.RemoveAll("_symlinks")
  247. os.Mkdir("_symlinks", 0755)
  248. os.Symlink("destination", "_symlinks/link")
  249. // Scan it
  250. fchan, err := Walk(Config{
  251. Dir: "_symlinks",
  252. BlockSize: 128 * 1024,
  253. })
  254. if err != nil {
  255. t.Fatal(err)
  256. }
  257. var files []protocol.FileInfo
  258. for f := range fchan {
  259. files = append(files, f)
  260. }
  261. // Verify that we got one symlink and with the correct attributes
  262. if len(files) != 1 {
  263. t.Errorf("expected 1 symlink, not %d", len(files))
  264. }
  265. if len(files[0].Blocks) != 0 {
  266. t.Errorf("expected zero blocks for symlink, not %d", len(files[0].Blocks))
  267. }
  268. if files[0].SymlinkTarget != "destination" {
  269. t.Errorf("expected symlink to have target destination, not %q", files[0].SymlinkTarget)
  270. }
  271. }
  272. func walkDir(dir string) ([]protocol.FileInfo, error) {
  273. fchan, err := Walk(Config{
  274. Dir: dir,
  275. BlockSize: 128 * 1024,
  276. AutoNormalize: true,
  277. Hashers: 2,
  278. })
  279. if err != nil {
  280. return nil, err
  281. }
  282. var tmp []protocol.FileInfo
  283. for f := range fchan {
  284. tmp = append(tmp, f)
  285. }
  286. sort.Sort(fileList(tmp))
  287. return tmp, nil
  288. }
  289. type fileList []protocol.FileInfo
  290. func (l fileList) Len() int {
  291. return len(l)
  292. }
  293. func (l fileList) Less(a, b int) bool {
  294. return l[a].Name < l[b].Name
  295. }
  296. func (l fileList) Swap(a, b int) {
  297. l[a], l[b] = l[b], l[a]
  298. }
  299. func (l fileList) testfiles() testfileList {
  300. testfiles := make(testfileList, len(l))
  301. for i, f := range l {
  302. if len(f.Blocks) > 1 {
  303. panic("simple test case stuff only supports a single block per file")
  304. }
  305. testfiles[i] = testfile{name: f.Name, length: f.FileSize()}
  306. if len(f.Blocks) == 1 {
  307. testfiles[i].hash = fmt.Sprintf("%x", f.Blocks[0].Hash)
  308. }
  309. }
  310. return testfiles
  311. }
  312. func (l testfileList) String() string {
  313. var b bytes.Buffer
  314. b.WriteString("{\n")
  315. for _, f := range l {
  316. fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.length, f.hash)
  317. }
  318. b.WriteString("}")
  319. return b.String()
  320. }
  321. var initOnce sync.Once
  322. const (
  323. testdataSize = 17 << 20
  324. testdataName = "_random.data"
  325. )
  326. func BenchmarkHashFile(b *testing.B) {
  327. initOnce.Do(initTestFile)
  328. b.ResetTimer()
  329. for i := 0; i < b.N; i++ {
  330. if _, err := HashFile(testdataName, protocol.BlockSize, nil, true); err != nil {
  331. b.Fatal(err)
  332. }
  333. }
  334. b.SetBytes(testdataSize)
  335. b.ReportAllocs()
  336. }
  337. func initTestFile() {
  338. fd, err := os.Create(testdataName)
  339. if err != nil {
  340. panic(err)
  341. }
  342. lr := io.LimitReader(rand.Reader, testdataSize)
  343. if _, err := io.Copy(fd, lr); err != nil {
  344. panic(err)
  345. }
  346. if err := fd.Close(); err != nil {
  347. panic(err)
  348. }
  349. }