filesystem_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2017 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 fs
  7. import (
  8. "path/filepath"
  9. "testing"
  10. )
  11. func TestIsInternal(t *testing.T) {
  12. cases := []struct {
  13. file string
  14. internal bool
  15. }{
  16. {".stfolder", true},
  17. {".stignore", true},
  18. {".stversions", true},
  19. {".stfolder/foo", true},
  20. {".stignore/foo", true},
  21. {".stversions/foo", true},
  22. {".stfolderfoo", false},
  23. {".stignorefoo", false},
  24. {".stversionsfoo", false},
  25. {"foo.stfolder", false},
  26. {"foo.stignore", false},
  27. {"foo.stversions", false},
  28. {"foo/.stfolder", false},
  29. {"foo/.stignore", false},
  30. {"foo/.stversions", false},
  31. }
  32. for _, tc := range cases {
  33. res := IsInternal(filepath.FromSlash(tc.file))
  34. if res != tc.internal {
  35. t.Errorf("Unexpected result: IsInteral(%q): %v should be %v", tc.file, res, tc.internal)
  36. }
  37. }
  38. }
  39. func TestCanonicalize(t *testing.T) {
  40. type testcase struct {
  41. path string
  42. expected string
  43. ok bool
  44. }
  45. cases := []testcase{
  46. // Valid cases
  47. {"/bar", "bar", true},
  48. {"/bar/baz", "bar/baz", true},
  49. {"bar", "bar", true},
  50. {"bar/baz", "bar/baz", true},
  51. // Not escape attempts, but oddly formatted relative paths
  52. {"", ".", true},
  53. {"/", ".", true},
  54. {"/..", ".", true},
  55. {"./bar", "bar", true},
  56. {"./bar/baz", "bar/baz", true},
  57. {"bar/../baz", "baz", true},
  58. {"/bar/../baz", "baz", true},
  59. {"./bar/../baz", "baz", true},
  60. // Results in an allowed path, but does it by probing. Disallowed.
  61. {"../foo", "", false},
  62. {"../foo/bar", "", false},
  63. {"../foo/bar", "", false},
  64. {"../../baz/foo/bar", "", false},
  65. {"bar/../../foo/bar", "", false},
  66. {"bar/../../../baz/foo/bar", "", false},
  67. // Escape attempts.
  68. {"..", "", false},
  69. {"../", "", false},
  70. {"../bar", "", false},
  71. {"../foobar", "", false},
  72. {"bar/../../quux/baz", "", false},
  73. }
  74. for _, tc := range cases {
  75. res, err := Canonicalize(tc.path)
  76. if tc.ok {
  77. if err != nil {
  78. t.Errorf("Unexpected error for Canonicalize(%q): %v", tc.path, err)
  79. continue
  80. }
  81. exp := filepath.FromSlash(tc.expected)
  82. if res != exp {
  83. t.Errorf("Unexpected result for Canonicalize(%q): %q != expected %q", tc.path, res, exp)
  84. }
  85. } else if err == nil {
  86. t.Errorf("Unexpected pass for Canonicalize(%q) => %q", tc.path, res)
  87. continue
  88. }
  89. }
  90. }
  91. func TestFileModeString(t *testing.T) {
  92. var fm FileMode = 0777
  93. exp := "-rwxrwxrwx"
  94. if fm.String() != exp {
  95. t.Fatalf("Got %v, expected %v", fm.String(), exp)
  96. }
  97. }