traversessymlink_test.go 2.5 KB

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