walk_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 https://mozilla.org/MPL/2.0/.
  6. package scanner
  7. import (
  8. "bytes"
  9. "context"
  10. "crypto/rand"
  11. "fmt"
  12. "io"
  13. "os"
  14. "path/filepath"
  15. "runtime"
  16. rdebug "runtime/debug"
  17. "sort"
  18. "sync"
  19. "testing"
  20. "github.com/d4l3k/messagediff"
  21. "github.com/syncthing/syncthing/lib/fs"
  22. "github.com/syncthing/syncthing/lib/ignore"
  23. "github.com/syncthing/syncthing/lib/osutil"
  24. "github.com/syncthing/syncthing/lib/protocol"
  25. "golang.org/x/text/unicode/norm"
  26. )
  27. type testfile struct {
  28. name string
  29. length int64
  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. func init() {
  43. // This test runs the risk of entering infinite recursion if it fails.
  44. // Limit the stack size to 10 megs to crash early in that case instead of
  45. // potentially taking down the box...
  46. rdebug.SetMaxStack(10 * 1 << 20)
  47. }
  48. func TestWalkSub(t *testing.T) {
  49. ignores := ignore.New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."))
  50. err := ignores.Load("testdata/.stignore")
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. fchan, err := Walk(context.TODO(), Config{
  55. Filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"),
  56. Subs: []string{"dir2"},
  57. BlockSize: 128 * 1024,
  58. Matcher: ignores,
  59. Hashers: 2,
  60. })
  61. var files []protocol.FileInfo
  62. for f := range fchan {
  63. files = append(files, f)
  64. }
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. // The directory contains two files, where one is ignored from a higher
  69. // level. We should see only the directory and one of the files.
  70. if len(files) != 2 {
  71. t.Fatalf("Incorrect length %d != 2", len(files))
  72. }
  73. if files[0].Name != "dir2" {
  74. t.Errorf("Incorrect file %v != dir2", files[0])
  75. }
  76. if files[1].Name != filepath.Join("dir2", "cfile") {
  77. t.Errorf("Incorrect file %v != dir2/cfile", files[1])
  78. }
  79. }
  80. func TestWalk(t *testing.T) {
  81. ignores := ignore.New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."))
  82. err := ignores.Load("testdata/.stignore")
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. t.Log(ignores)
  87. fchan, err := Walk(context.TODO(), Config{
  88. Filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"),
  89. BlockSize: 128 * 1024,
  90. Matcher: ignores,
  91. Hashers: 2,
  92. })
  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 diff, equal := messagediff.PrettyDiff(testdata, files); !equal {
  103. t.Errorf("Walk returned unexpected data. Diff:\n%s", diff)
  104. }
  105. }
  106. func TestWalkError(t *testing.T) {
  107. _, err := Walk(context.TODO(), Config{
  108. Filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata-missing"),
  109. BlockSize: 128 * 1024,
  110. Hashers: 2,
  111. })
  112. if err == nil {
  113. t.Error("no error from missing directory")
  114. }
  115. _, err = Walk(context.TODO(), Config{
  116. Filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata/bar"),
  117. BlockSize: 128 * 1024,
  118. })
  119. if err == nil {
  120. t.Error("no error from non-directory")
  121. }
  122. }
  123. func TestVerify(t *testing.T) {
  124. blocksize := 16
  125. // data should be an even multiple of blocksize long
  126. data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e")
  127. buf := bytes.NewBuffer(data)
  128. progress := newByteCounter()
  129. defer progress.Close()
  130. blocks, err := Blocks(context.TODO(), buf, blocksize, -1, progress, false)
  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. if int64(len(data)) != progress.Total() {
  138. t.Fatalf("Incorrect counter value %d != %d", len(data), progress.Total())
  139. }
  140. buf = bytes.NewBuffer(data)
  141. err = Verify(buf, blocksize, blocks)
  142. t.Log(err)
  143. if err != nil {
  144. t.Fatal("Unexpected verify failure", err)
  145. }
  146. buf = bytes.NewBuffer(append(data, '\n'))
  147. err = Verify(buf, blocksize, blocks)
  148. t.Log(err)
  149. if err == nil {
  150. t.Fatal("Unexpected verify success")
  151. }
  152. buf = bytes.NewBuffer(data[:len(data)-1])
  153. err = Verify(buf, blocksize, blocks)
  154. t.Log(err)
  155. if err == nil {
  156. t.Fatal("Unexpected verify success")
  157. }
  158. data[42] = 42
  159. buf = bytes.NewBuffer(data)
  160. err = Verify(buf, blocksize, blocks)
  161. t.Log(err)
  162. if err == nil {
  163. t.Fatal("Unexpected verify success")
  164. }
  165. }
  166. func TestNormalization(t *testing.T) {
  167. if runtime.GOOS == "darwin" {
  168. t.Skip("Normalization test not possible on darwin")
  169. return
  170. }
  171. os.RemoveAll("testdata/normalization")
  172. defer os.RemoveAll("testdata/normalization")
  173. tests := []string{
  174. "0-A", // ASCII A -- accepted
  175. "1-\xC3\x84", // NFC 'Ä' -- conflicts with the entry below, accepted
  176. "1-\x41\xCC\x88", // NFD 'Ä' -- conflicts with the entry above, ignored
  177. "2-\xC3\x85", // NFC 'Å' -- accepted
  178. "3-\x41\xCC\x83", // NFD 'Ã' -- converted to NFC
  179. "4-\xE2\x98\x95", // U+2615 HOT BEVERAGE (☕) -- accepted
  180. "5-\xCD\xE2", // EUC-CN "wài" (外) -- ignored (not UTF8)
  181. }
  182. numInvalid := 2
  183. if runtime.GOOS == "windows" {
  184. // On Windows, in case 5 the character gets replaced with a
  185. // replacement character \xEF\xBF\xBD at the point it's written to disk,
  186. // which means it suddenly becomes valid (sort of).
  187. numInvalid--
  188. }
  189. numValid := len(tests) - numInvalid
  190. fs := fs.NewFilesystem(fs.FilesystemTypeBasic, ".")
  191. for _, s1 := range tests {
  192. // Create a directory for each of the interesting strings above
  193. if err := fs.MkdirAll(filepath.Join("testdata/normalization", s1), 0755); err != nil {
  194. t.Fatal(err)
  195. }
  196. for _, s2 := range tests {
  197. // Within each dir, create a file with each of the interesting
  198. // file names. Ensure that the file doesn't exist when it's
  199. // created. This detects and fails if there's file name
  200. // normalization stuff at the filesystem level.
  201. if fd, err := fs.OpenFile(filepath.Join("testdata/normalization", s1, s2), os.O_CREATE|os.O_EXCL, 0644); err != nil {
  202. t.Fatal(err)
  203. } else {
  204. fd.Write([]byte("test"))
  205. fd.Close()
  206. }
  207. }
  208. }
  209. // We can normalize a directory name, but we can't descend into it in the
  210. // same pass due to how filepath.Walk works. So we run the scan twice to
  211. // make sure it all gets done. In production, things will be correct
  212. // eventually...
  213. _, err := walkDir(fs, "testdata/normalization")
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. tmp, err := walkDir(fs, "testdata/normalization")
  218. if err != nil {
  219. t.Fatal(err)
  220. }
  221. files := fileList(tmp).testfiles()
  222. // We should have one file per combination, plus the directories
  223. // themselves, plus the "testdata/normalization" directory
  224. expectedNum := numValid*numValid + numValid + 1
  225. if len(files) != expectedNum {
  226. t.Errorf("Expected %d files, got %d", expectedNum, len(files))
  227. }
  228. // The file names should all be in NFC form.
  229. for _, f := range files {
  230. t.Logf("%q (% x) %v", f.name, f.name, norm.NFC.IsNormalString(f.name))
  231. if !norm.NFC.IsNormalString(f.name) {
  232. t.Errorf("File name %q is not NFC normalized", f.name)
  233. }
  234. }
  235. }
  236. func TestIssue1507(t *testing.T) {
  237. w := &walker{}
  238. c := make(chan protocol.FileInfo, 100)
  239. fn := w.walkAndHashFiles(context.TODO(), c, c)
  240. fn("", nil, protocol.ErrClosed)
  241. }
  242. func TestWalkSymlinkUnix(t *testing.T) {
  243. if runtime.GOOS == "windows" {
  244. t.Skip("skipping unsupported symlink test")
  245. return
  246. }
  247. // Create a folder with a symlink in it
  248. os.RemoveAll("_symlinks")
  249. defer os.RemoveAll("_symlinks")
  250. os.Mkdir("_symlinks", 0755)
  251. os.Symlink("destination", "_symlinks/link")
  252. // Scan it
  253. fchan, err := Walk(context.TODO(), Config{
  254. Filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "_symlinks"),
  255. BlockSize: 128 * 1024,
  256. })
  257. if err != nil {
  258. t.Fatal(err)
  259. }
  260. var files []protocol.FileInfo
  261. for f := range fchan {
  262. files = append(files, f)
  263. }
  264. // Verify that we got one symlink and with the correct attributes
  265. if len(files) != 1 {
  266. t.Errorf("expected 1 symlink, not %d", len(files))
  267. }
  268. if len(files[0].Blocks) != 0 {
  269. t.Errorf("expected zero blocks for symlink, not %d", len(files[0].Blocks))
  270. }
  271. if files[0].SymlinkTarget != "destination" {
  272. t.Errorf("expected symlink to have target destination, not %q", files[0].SymlinkTarget)
  273. }
  274. }
  275. func TestWalkSymlinkWindows(t *testing.T) {
  276. if runtime.GOOS != "windows" {
  277. t.Skip("skipping unsupported symlink test")
  278. }
  279. // Create a folder with a symlink in it
  280. os.RemoveAll("_symlinks")
  281. defer os.RemoveAll("_symlinks")
  282. os.Mkdir("_symlinks", 0755)
  283. if err := osutil.DebugSymlinkForTestsOnly("destination", "_symlinks/link"); err != nil {
  284. // Probably we require permissions we don't have.
  285. t.Skip(err)
  286. }
  287. // Scan it
  288. fchan, err := Walk(context.TODO(), Config{
  289. Filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "_symlinks"),
  290. BlockSize: 128 * 1024,
  291. })
  292. if err != nil {
  293. t.Fatal(err)
  294. }
  295. var files []protocol.FileInfo
  296. for f := range fchan {
  297. files = append(files, f)
  298. }
  299. // Verify that we got zero symlinks
  300. if len(files) != 0 {
  301. t.Errorf("expected zero symlinks, not %d", len(files))
  302. }
  303. }
  304. func walkDir(fs fs.Filesystem, dir string) ([]protocol.FileInfo, error) {
  305. fchan, err := Walk(context.TODO(), Config{
  306. Filesystem: fs,
  307. Subs: []string{dir},
  308. BlockSize: 128 * 1024,
  309. AutoNormalize: true,
  310. Hashers: 2,
  311. })
  312. if err != nil {
  313. return nil, err
  314. }
  315. var tmp []protocol.FileInfo
  316. for f := range fchan {
  317. tmp = append(tmp, f)
  318. }
  319. sort.Sort(fileList(tmp))
  320. return tmp, nil
  321. }
  322. type fileList []protocol.FileInfo
  323. func (l fileList) Len() int {
  324. return len(l)
  325. }
  326. func (l fileList) Less(a, b int) bool {
  327. return l[a].Name < l[b].Name
  328. }
  329. func (l fileList) Swap(a, b int) {
  330. l[a], l[b] = l[b], l[a]
  331. }
  332. func (l fileList) testfiles() testfileList {
  333. testfiles := make(testfileList, len(l))
  334. for i, f := range l {
  335. if len(f.Blocks) > 1 {
  336. panic("simple test case stuff only supports a single block per file")
  337. }
  338. testfiles[i] = testfile{name: f.Name, length: f.FileSize()}
  339. if len(f.Blocks) == 1 {
  340. testfiles[i].hash = fmt.Sprintf("%x", f.Blocks[0].Hash)
  341. }
  342. }
  343. return testfiles
  344. }
  345. func (l testfileList) String() string {
  346. var b bytes.Buffer
  347. b.WriteString("{\n")
  348. for _, f := range l {
  349. fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.length, f.hash)
  350. }
  351. b.WriteString("}")
  352. return b.String()
  353. }
  354. var initOnce sync.Once
  355. const (
  356. testdataSize = 17 << 20
  357. testdataName = "_random.data"
  358. )
  359. func BenchmarkHashFile(b *testing.B) {
  360. initOnce.Do(initTestFile)
  361. b.ResetTimer()
  362. for i := 0; i < b.N; i++ {
  363. if _, err := HashFile(context.TODO(), fs.NewFilesystem(fs.FilesystemTypeBasic, ""), testdataName, protocol.BlockSize, nil, true); err != nil {
  364. b.Fatal(err)
  365. }
  366. }
  367. b.SetBytes(testdataSize)
  368. b.ReportAllocs()
  369. }
  370. func initTestFile() {
  371. fd, err := os.Create(testdataName)
  372. if err != nil {
  373. panic(err)
  374. }
  375. lr := io.LimitReader(rand.Reader, testdataSize)
  376. if _, err := io.Copy(fd, lr); err != nil {
  377. panic(err)
  378. }
  379. if err := fd.Close(); err != nil {
  380. panic(err)
  381. }
  382. }
  383. func TestStopWalk(t *testing.T) {
  384. // Create tree that is 100 levels deep, with each level containing 100
  385. // files (each 1 MB) and 100 directories (in turn containing 100 files
  386. // and 100 directories, etc). That is, in total > 100^100 files and as
  387. // many directories. It'll take a while to scan, giving us time to
  388. // cancel it and make sure the scan stops.
  389. // Use an errorFs as the backing fs for the rest of the interface
  390. // The way we get it is a bit hacky tho.
  391. errorFs := fs.NewFilesystem(fs.FilesystemType(-1), ".")
  392. fs := fs.NewWalkFilesystem(&infiniteFS{errorFs, 100, 100, 1e6})
  393. const numHashers = 4
  394. ctx, cancel := context.WithCancel(context.Background())
  395. fchan, err := Walk(ctx, Config{
  396. Filesystem: fs,
  397. BlockSize: 128 * 1024,
  398. Hashers: numHashers,
  399. ProgressTickIntervalS: -1, // Don't attempt to build the full list of files before starting to scan...
  400. })
  401. if err != nil {
  402. t.Fatal(err)
  403. }
  404. // Receive a few entries to make sure the walker is up and running,
  405. // scanning both files and dirs. Do some quick sanity tests on the
  406. // returned file entries to make sure we are not just reading crap from
  407. // a closed channel or something.
  408. dirs := 0
  409. files := 0
  410. for {
  411. f := <-fchan
  412. t.Log("Scanned", f)
  413. if f.IsDirectory() {
  414. if len(f.Name) == 0 || f.Permissions == 0 {
  415. t.Error("Bad directory entry", f)
  416. }
  417. dirs++
  418. } else {
  419. if len(f.Name) == 0 || len(f.Blocks) == 0 || f.Permissions == 0 {
  420. t.Error("Bad file entry", f)
  421. }
  422. files++
  423. }
  424. if dirs > 5 && files > 5 {
  425. break
  426. }
  427. }
  428. // Cancel the walker.
  429. cancel()
  430. // Empty out any waiting entries and wait for the channel to close.
  431. // Count them, they should be zero or very few - essentially, each
  432. // hasher has the choice of returning a fully handled entry or
  433. // cancelling, but they should not start on another item.
  434. extra := 0
  435. for range fchan {
  436. extra++
  437. }
  438. t.Log("Extra entries:", extra)
  439. if extra > numHashers {
  440. t.Error("unexpected extra entries received after cancel")
  441. }
  442. }