traversessymlink_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2016 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 osutil_test
  7. import (
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. "github.com/syncthing/syncthing/lib/build"
  12. "github.com/syncthing/syncthing/lib/fs"
  13. "github.com/syncthing/syncthing/lib/osutil"
  14. )
  15. func TestTraversesSymlink(t *testing.T) {
  16. tmpDir := t.TempDir()
  17. testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir)
  18. testFs.MkdirAll("a/b/c", 0755)
  19. if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil {
  20. if build.IsWindows {
  21. t.Skip("Symlinks aren't working")
  22. }
  23. t.Fatal(err)
  24. }
  25. // a/l -> b, so a/l/c should resolve by normal stat
  26. info, err := testFs.Lstat("a/l/c")
  27. if err != nil {
  28. t.Fatal("unexpected error", err)
  29. }
  30. if !info.IsDir() {
  31. t.Fatal("error in setup, a/l/c should be a directory")
  32. }
  33. cases := []struct {
  34. name string
  35. traverses bool
  36. }{
  37. // Exist
  38. {".", false},
  39. {"a", false},
  40. {"a/b", false},
  41. {"a/b/c", false},
  42. // Don't exist
  43. {"x", false},
  44. {"a/x", false},
  45. {"a/b/x", false},
  46. {"a/x/c", false},
  47. // Symlink or behind symlink
  48. {"a/l", true},
  49. {"a/l/c", true},
  50. // Non-existing behind a symlink
  51. {"a/l/x", true},
  52. }
  53. for _, tc := range cases {
  54. if res := osutil.TraversesSymlink(testFs, tc.name); tc.traverses == (res == nil) {
  55. t.Errorf("TraversesSymlink(%q) = %v, should be %v", tc.name, res, tc.traverses)
  56. }
  57. }
  58. }
  59. func TestIssue4875(t *testing.T) {
  60. tmpDir := t.TempDir()
  61. testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir)
  62. testFs.MkdirAll(filepath.Join("a", "b", "c"), 0755)
  63. if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil {
  64. if build.IsWindows {
  65. t.Skip("Symlinks aren't working")
  66. }
  67. t.Fatal(err)
  68. }
  69. // a/l -> b, so a/l/c should resolve by normal stat
  70. info, err := testFs.Lstat("a/l/c")
  71. if err != nil {
  72. t.Fatal("unexpected error", err)
  73. }
  74. if !info.IsDir() {
  75. t.Fatal("error in setup, a/l/c should be a directory")
  76. }
  77. testFs = fs.NewFilesystem(fs.FilesystemTypeBasic, filepath.Join(tmpDir, "a/l"))
  78. if err := osutil.TraversesSymlink(testFs, "."); err != nil {
  79. t.Error(`TraversesSymlink on filesystem with symlink at root returned error for ".":`, err)
  80. }
  81. }
  82. var traversesSymlinkResult error
  83. func BenchmarkTraversesSymlink(b *testing.B) {
  84. os.RemoveAll("testdata")
  85. defer os.RemoveAll("testdata")
  86. fs := fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
  87. fs.MkdirAll("a/b/c", 0755)
  88. for i := 0; i < b.N; i++ {
  89. traversesSymlinkResult = osutil.TraversesSymlink(fs, "a/b/c")
  90. }
  91. b.ReportAllocs()
  92. }