traversessymlink_test.go 3.0 KB

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