traversessymlink_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // +build !windows
  7. package osutil_test
  8. import (
  9. "os"
  10. "testing"
  11. "github.com/syncthing/syncthing/lib/fs"
  12. "github.com/syncthing/syncthing/lib/osutil"
  13. )
  14. func TestTraversesSymlink(t *testing.T) {
  15. os.RemoveAll("testdata")
  16. defer os.RemoveAll("testdata")
  17. fs := fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
  18. fs.MkdirAll("a/b/c", 0755)
  19. fs.CreateSymlink("b", "a/l")
  20. // a/l -> b, so a/l/c should resolve by normal stat
  21. info, err := fs.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(fs, tc.name); tc.traverses == (res == nil) {
  50. t.Errorf("TraversesSymlink(%q) = %v, should be %v", tc.name, res, tc.traverses)
  51. }
  52. }
  53. }
  54. var traversesSymlinkResult error
  55. func BenchmarkTraversesSymlink(b *testing.B) {
  56. os.RemoveAll("testdata")
  57. defer os.RemoveAll("testdata")
  58. fs := fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
  59. fs.MkdirAll("a/b/c", 0755)
  60. for i := 0; i < b.N; i++ {
  61. traversesSymlinkResult = osutil.TraversesSymlink(fs, "a/b/c")
  62. }
  63. b.ReportAllocs()
  64. }