walkfs_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (C) 2019 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 fs
  7. import (
  8. "fmt"
  9. osexec "os/exec"
  10. "path/filepath"
  11. "runtime"
  12. "testing"
  13. )
  14. func testWalkSkipSymlink(t *testing.T, fsType FilesystemType, uri string) {
  15. if runtime.GOOS == "windows" {
  16. t.Skip("Symlinks skipping is not tested on windows")
  17. }
  18. fs := NewFilesystem(fsType, uri)
  19. if err := fs.MkdirAll("target/foo", 0755); err != nil {
  20. t.Fatal(err)
  21. }
  22. if err := fs.Mkdir("towalk", 0755); err != nil {
  23. t.Fatal(err)
  24. }
  25. if err := fs.CreateSymlink("target", "towalk/symlink"); err != nil {
  26. t.Fatal(err)
  27. }
  28. if err := fs.Walk("towalk", func(path string, info FileInfo, err error) error {
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. if info.Name() != "symlink" && info.Name() != "towalk" {
  33. t.Fatal("Walk unexpected file", info.Name())
  34. }
  35. return nil
  36. }); err != nil {
  37. t.Fatal(err)
  38. }
  39. }
  40. func createDirJunct(target string, name string) error {
  41. output, err := osexec.Command("cmd", "/c", "mklink", "/J", name, target).CombinedOutput()
  42. if err != nil {
  43. return fmt.Errorf("Failed to run mklink %v %v: %v %q", name, target, err, output)
  44. }
  45. return nil
  46. }
  47. func testWalkTraverseDirJunct(t *testing.T, fsType FilesystemType, uri string) {
  48. if runtime.GOOS != "windows" {
  49. t.Skip("Directory junctions are available and tested on windows only")
  50. }
  51. fs := NewFilesystem(fsType, uri)
  52. if err := fs.MkdirAll("target/foo", 0); err != nil {
  53. t.Fatal(err)
  54. }
  55. if err := fs.Mkdir("towalk", 0); err != nil {
  56. t.Fatal(err)
  57. }
  58. if err := createDirJunct(filepath.Join(uri, "target"), filepath.Join(uri, "towalk/dirjunct")); err != nil {
  59. t.Fatal(err)
  60. }
  61. traversed := false
  62. if err := fs.Walk("towalk", func(path string, info FileInfo, err error) error {
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. if info.Name() == "foo" {
  67. traversed = true
  68. }
  69. return nil
  70. }); err != nil {
  71. t.Fatal(err)
  72. }
  73. if !traversed {
  74. t.Fatal("Directory junction was not traversed")
  75. }
  76. }
  77. func testWalkInfiniteRecursion(t *testing.T, fsType FilesystemType, uri string) {
  78. if runtime.GOOS != "windows" {
  79. t.Skip("Infinite recursion detection is tested on windows only")
  80. }
  81. fs := NewFilesystem(fsType, uri)
  82. if err := fs.MkdirAll("target/foo", 0); err != nil {
  83. t.Fatal(err)
  84. }
  85. if err := fs.Mkdir("towalk", 0); err != nil {
  86. t.Fatal(err)
  87. }
  88. if err := createDirJunct(filepath.Join(uri, "target"), filepath.Join(uri, "towalk/dirjunct")); err != nil {
  89. t.Fatal(err)
  90. }
  91. if err := createDirJunct(filepath.Join(uri, "towalk"), filepath.Join(uri, "target/foo/recurse")); err != nil {
  92. t.Fatal(err)
  93. }
  94. dirjunctCnt := 0
  95. fooCnt := 0
  96. if err := fs.Walk("towalk", func(path string, info FileInfo, err error) error {
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if info.Name() == "dirjunct" {
  101. dirjunctCnt++
  102. } else if info.Name() == "foo" {
  103. fooCnt++
  104. }
  105. return nil
  106. }); err != nil {
  107. t.Fatal(err)
  108. }
  109. if dirjunctCnt != 2 || fooCnt != 1 {
  110. t.Fatal("Infinite recursion not detected correctly")
  111. }
  112. }