filesystem_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. "fmt"
  9. "path/filepath"
  10. "testing"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/build"
  13. )
  14. func TestIsInternal(t *testing.T) {
  15. cases := []struct {
  16. file string
  17. internal bool
  18. }{
  19. {".stfolder", true},
  20. {".stignore", true},
  21. {".stversions", true},
  22. {".stfolder/foo", true},
  23. {".stignore/foo", true},
  24. {".stversions/foo", true},
  25. {".stfolderfoo", false},
  26. {".stignorefoo", false},
  27. {".stversionsfoo", false},
  28. {"foo.stfolder", false},
  29. {"foo.stignore", false},
  30. {"foo.stversions", false},
  31. {"foo/.stfolder", false},
  32. {"foo/.stignore", false},
  33. {"foo/.stversions", false},
  34. }
  35. for _, tc := range cases {
  36. res := IsInternal(filepath.FromSlash(tc.file))
  37. if res != tc.internal {
  38. t.Errorf("Unexpected result: IsInternal(%q): %v should be %v", tc.file, res, tc.internal)
  39. }
  40. }
  41. }
  42. func TestCanonicalize(t *testing.T) {
  43. type testcase struct {
  44. path string
  45. expected string
  46. ok bool
  47. }
  48. cases := []testcase{
  49. // Valid cases
  50. {"/bar", "bar", true},
  51. {"/bar/baz", "bar/baz", true},
  52. {"bar", "bar", true},
  53. {"bar/baz", "bar/baz", true},
  54. // Not escape attempts, but oddly formatted relative paths
  55. {"", ".", true},
  56. {"/", ".", true},
  57. {"/..", ".", true},
  58. {"./bar", "bar", true},
  59. {"./bar/baz", "bar/baz", true},
  60. {"bar/../baz", "baz", true},
  61. {"/bar/../baz", "baz", true},
  62. {"./bar/../baz", "baz", true},
  63. // Results in an allowed path, but does it by probing. Disallowed.
  64. {"../foo", "", false},
  65. {"../foo/bar", "", false},
  66. {"../foo/bar", "", false},
  67. {"../../baz/foo/bar", "", false},
  68. {"bar/../../foo/bar", "", false},
  69. {"bar/../../../baz/foo/bar", "", false},
  70. // Escape attempts.
  71. {"..", "", false},
  72. {"../", "", false},
  73. {"../bar", "", false},
  74. {"../foobar", "", false},
  75. {"bar/../../quux/baz", "", false},
  76. }
  77. for _, tc := range cases {
  78. res, err := Canonicalize(tc.path)
  79. if tc.ok {
  80. if err != nil {
  81. t.Errorf("Unexpected error for Canonicalize(%q): %v", tc.path, err)
  82. continue
  83. }
  84. exp := filepath.FromSlash(tc.expected)
  85. if res != exp {
  86. t.Errorf("Unexpected result for Canonicalize(%q): %q != expected %q", tc.path, res, exp)
  87. }
  88. } else if err == nil {
  89. t.Errorf("Unexpected pass for Canonicalize(%q) => %q", tc.path, res)
  90. continue
  91. }
  92. }
  93. }
  94. func TestFileModeString(t *testing.T) {
  95. var fm FileMode = 0o777
  96. exp := "-rwxrwxrwx"
  97. if fm.String() != exp {
  98. t.Fatalf("Got %v, expected %v", fm.String(), exp)
  99. }
  100. }
  101. func TestIsParent(t *testing.T) {
  102. test := func(path, parent string, expected bool) {
  103. t.Helper()
  104. path = filepath.FromSlash(path)
  105. parent = filepath.FromSlash(parent)
  106. if res := IsParent(path, parent); res != expected {
  107. t.Errorf(`Unexpected result: IsParent("%v", "%v"): %v should be %v`, path, parent, res, expected)
  108. }
  109. }
  110. testBoth := func(path, parent string, expected bool) {
  111. t.Helper()
  112. test(path, parent, expected)
  113. if build.IsWindows {
  114. test("C:/"+path, "C:/"+parent, expected)
  115. } else {
  116. test("/"+path, "/"+parent, expected)
  117. }
  118. }
  119. // rel - abs
  120. for _, parent := range []string{"/", "/foo", "/foo/bar"} {
  121. for _, path := range []string{"", ".", "foo", "foo/bar", "bas", "bas/baz"} {
  122. if build.IsWindows {
  123. parent = "C:/" + parent
  124. }
  125. test(parent, path, false)
  126. test(path, parent, false)
  127. }
  128. }
  129. // equal
  130. for i, path := range []string{"/", "/foo", "/foo/bar", "", ".", "foo", "foo/bar"} {
  131. if i < 3 && build.IsWindows {
  132. path = "C:" + path
  133. }
  134. test(path, path, false)
  135. }
  136. test("", ".", false)
  137. test(".", "", false)
  138. for _, parent := range []string{"", "."} {
  139. for _, path := range []string{"foo", "foo/bar"} {
  140. test(path, parent, true)
  141. test(parent, path, false)
  142. }
  143. }
  144. for _, parent := range []string{"foo", "foo/bar"} {
  145. for _, path := range []string{"bar", "bar/foo"} {
  146. testBoth(path, parent, false)
  147. testBoth(parent, path, false)
  148. }
  149. }
  150. for _, parent := range []string{"foo", "foo/bar"} {
  151. for _, path := range []string{"foo/bar/baz", "foo/bar/baz/bas"} {
  152. testBoth(path, parent, true)
  153. testBoth(parent, path, false)
  154. if build.IsWindows {
  155. test("C:/"+path, "D:/"+parent, false)
  156. }
  157. }
  158. }
  159. }
  160. // Reproduces issue 9677:
  161. // The combination of caching the entire case FS and moving the case FS to be
  162. // the outmost layer of the FS lead to the mtime FS disappearing. This is
  163. // because in many places we intentionally create the filesystem without access
  164. // to the DB and thus without the mtime FS layer. With the case FS layer
  165. // outside, all the inner layers are also cached - notable without an mtime FS
  166. // layer. Later when we do try to create an FS with DB/mtime FS, we still get
  167. // the cached FS without mtime FS.
  168. func TestRepro9677MissingMtimeFS(t *testing.T) {
  169. mtimeDB := make(mapStore)
  170. name := "Testfile"
  171. nameLower := UnicodeLowercaseNormalized(name)
  172. testTime := time.Unix(1723491493, 123456789)
  173. // Create a file with an mtime FS entry
  174. firstFS := NewFilesystem(FilesystemTypeFake, fmt.Sprintf("%v?insens=true&timeprecisionsecond=true", t.Name()), &OptionDetectCaseConflicts{}, NewMtimeOption(mtimeDB))
  175. // Create a file, set its mtime and check that we get the expected mtime when stat-ing.
  176. file, err := firstFS.Create(name)
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. file.Close()
  181. err = firstFS.Chtimes(name, testTime, testTime)
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. checkMtime := func(fs Filesystem) {
  186. t.Helper()
  187. info, err := fs.Lstat(name)
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. if !info.ModTime().Equal(testTime) {
  192. t.Errorf("Expected mtime %v for %v, got %v", testTime, name, info.ModTime())
  193. }
  194. info, err = fs.Lstat(nameLower)
  195. if !IsErrCaseConflict(err) {
  196. t.Errorf("Expected case-conflict error, got %v", err)
  197. }
  198. }
  199. checkMtime(firstFS)
  200. // Now syncthing gets upgraded (or even just restarted), which resets the
  201. // case FS registry as it lives in memory.
  202. globalCaseFilesystemRegistry = caseFilesystemRegistry{caseCaches: make(map[fskey]*caseCache)}
  203. // This time we first create some filesystem without a database and thus no
  204. // mtime-FS, which is used in various places outside of the folder code. We
  205. // aren't actually going to do anything, this just adds an entry to the
  206. // caseFS cache. And that's the crucial bit: In the broken case this test is
  207. // reproducing, it will add the FS without mtime-FS, so all future FSes will
  208. // be without mtime, even if requested:
  209. NewFilesystem(FilesystemTypeFake, fmt.Sprintf("%v?insens=true&timeprecisionsecond=true", t.Name()), &OptionDetectCaseConflicts{})
  210. newFS := NewFilesystem(FilesystemTypeFake, fmt.Sprintf("%v?insens=true&timeprecisionsecond=true", t.Name()), &OptionDetectCaseConflicts{}, NewMtimeOption(mtimeDB))
  211. checkMtime(newFS)
  212. }