traversessymlink_test.go 3.0 KB

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