simple_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright (C) 2014 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 versioner
  7. import (
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/syncthing/syncthing/lib/build"
  14. "github.com/syncthing/syncthing/lib/config"
  15. )
  16. func TestTaggedFilename(t *testing.T) {
  17. cases := [][3]string{
  18. {filepath.Join("foo", "bar.baz"), "tag", filepath.Join("foo", "bar~tag.baz")},
  19. {"bar.baz", "tag", "bar~tag.baz"},
  20. {"bar", "tag", "bar~tag"},
  21. {"~$ufheft2.docx", "20140612-200554", "~$ufheft2~20140612-200554.docx"},
  22. {"alle~4.mgz", "20141106-094415", "alle~4~20141106-094415.mgz"},
  23. // Parsing test only
  24. {"", "tag-only", "foo/bar.baz~tag-only"},
  25. {"", "tag-only", "bar.baz~tag-only"},
  26. {"", "20140612-200554", "~$ufheft2.docx~20140612-200554"},
  27. {"", "20141106-094415", "alle~4.mgz~20141106-094415"},
  28. }
  29. for _, tc := range cases {
  30. if tc[0] != "" {
  31. // Test tagger
  32. tf := TagFilename(tc[0], tc[1])
  33. if tf != tc[2] {
  34. t.Errorf("%s != %s", tf, tc[2])
  35. }
  36. }
  37. // Test parser
  38. tag := extractTag(tc[2])
  39. if tag != tc[1] {
  40. t.Errorf("%s != %s", tag, tc[1])
  41. }
  42. }
  43. }
  44. func TestSimpleVersioningVersionCount(t *testing.T) {
  45. if testing.Short() {
  46. t.Skip("Test takes some time, skipping.")
  47. }
  48. dir := t.TempDir()
  49. cfg := config.FolderConfiguration{
  50. FilesystemType: config.FilesystemTypeBasic,
  51. Path: dir,
  52. Versioning: config.VersioningConfiguration{
  53. Params: map[string]string{
  54. "keep": "2",
  55. },
  56. },
  57. }
  58. fs := cfg.Filesystem()
  59. v := newSimple(cfg)
  60. path := "test"
  61. for i := 1; i <= 3; i++ {
  62. f, err := fs.Create(path)
  63. if err != nil {
  64. t.Error(err)
  65. }
  66. f.Close()
  67. if err := v.Archive(path); err != nil {
  68. t.Error(err)
  69. }
  70. n, err := fs.DirNames(DefaultPath)
  71. if err != nil {
  72. t.Error(err)
  73. }
  74. if len(n) != min(i, 2) {
  75. t.Error("Wrong count")
  76. }
  77. time.Sleep(time.Second)
  78. }
  79. }
  80. func TestPathTildes(t *testing.T) {
  81. // Test that folder and version paths with leading tildes are expanded
  82. // to the user's home directory. (issue #9241)
  83. home := t.TempDir()
  84. t.Setenv("HOME", home)
  85. if vn := filepath.VolumeName(home); vn != "" {
  86. // Legacy Windows home stuff
  87. t.Setenv("HomeDrive", vn)
  88. t.Setenv("HomePath", strings.TrimPrefix(home, vn))
  89. }
  90. os.Mkdir(filepath.Join(home, "folder"), 0o755)
  91. cfg := config.FolderConfiguration{
  92. FilesystemType: config.FilesystemTypeBasic,
  93. Path: "~/folder",
  94. Versioning: config.VersioningConfiguration{
  95. FSPath: "~/versions",
  96. FSType: config.FilesystemTypeBasic,
  97. Params: map[string]string{
  98. "keep": "2",
  99. },
  100. },
  101. }
  102. fs := cfg.Filesystem()
  103. v := newSimple(cfg)
  104. const testPath = "test"
  105. f, err := fs.Create(testPath)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. f.Close()
  110. if err := v.Archive(testPath); err != nil {
  111. t.Fatal(err)
  112. }
  113. // Check that there are no entries in the folder directory; this is
  114. // specifically to check that there is no directory named "~" there.
  115. names, err := fs.DirNames(".")
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. if len(names) != 0 {
  120. t.Fatalf("found %d files in folder dir, want 0", len(names))
  121. }
  122. // Check that the versions directory contains one file that begins with
  123. // our test path.
  124. des, err := os.ReadDir(filepath.Join(home, "versions"))
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. for _, de := range des {
  129. names = append(names, de.Name())
  130. }
  131. if len(names) != 1 {
  132. t.Fatalf("found %d files in versions dir, want 1", len(names))
  133. }
  134. if got := names[0]; !strings.HasPrefix(got, testPath) {
  135. t.Fatalf("found versioned file %q, want one that begins with %q", got, testPath)
  136. }
  137. }
  138. func TestArchiveFoldersCreationPermission(t *testing.T) {
  139. if build.IsWindows {
  140. t.Skip("Skipping on Windows")
  141. return
  142. }
  143. dir := t.TempDir()
  144. versionsDir := t.TempDir()
  145. cfg := config.FolderConfiguration{
  146. FilesystemType: config.FilesystemTypeBasic,
  147. Path: dir,
  148. Versioning: config.VersioningConfiguration{
  149. FSPath: versionsDir,
  150. FSType: config.FilesystemTypeBasic,
  151. Params: map[string]string{
  152. "keep": "2",
  153. },
  154. },
  155. }
  156. vfs := cfg.Filesystem()
  157. v := newSimple(cfg)
  158. // Create two folders and set their permissions
  159. folder1Path := filepath.Join(dir, "folder1")
  160. folder1Perms := os.FileMode(0o777)
  161. folder1VersionsPath := filepath.Join(versionsDir, "folder1")
  162. err := os.Mkdir(folder1Path, folder1Perms)
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. // chmod incase umask changes the create permissions
  167. err = os.Chmod(folder1Path, folder1Perms)
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. folder2Path := filepath.Join(folder1Path, "földer2")
  172. folder2VersionsPath := filepath.Join(folder1VersionsPath, "földer2")
  173. folder2Perms := os.FileMode(0o744)
  174. err = os.Mkdir(folder2Path, folder2Perms)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. // chmod incase umask changes the create permissions
  179. err = os.Chmod(folder2Path, folder2Perms)
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. // create a file
  184. filePath := filepath.Join("folder1", "földer2", "testFile")
  185. f, err := vfs.Create(filePath)
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. f.Close()
  190. if err := v.Archive(filePath); err != nil {
  191. t.Error(err)
  192. }
  193. // check permissions of the created version folders
  194. folder1VersionsInfo, err := os.Stat(folder1VersionsPath)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. if folder1VersionsInfo.Mode().Perm() != folder1Perms {
  199. t.Errorf("folder1 permissions %v, want %v", folder1VersionsInfo.Mode(), folder1Perms)
  200. }
  201. folder2VersionsInfo, err := os.Stat(folder2VersionsPath)
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. if folder2VersionsInfo.Mode().Perm() != folder2Perms {
  206. t.Errorf("földer2 permissions %v, want %v", folder2VersionsInfo.Mode(), folder2Perms)
  207. }
  208. // Archive again to test that archiving doesn't fail if the versioned folders already exist
  209. if err := v.Archive(filePath); err != nil {
  210. t.Error(err)
  211. }
  212. folder1VersionsInfo, err = os.Stat(folder1VersionsPath)
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. if folder1VersionsInfo.Mode().Perm() != folder1Perms {
  217. t.Errorf("folder1 permissions %v, want %v", folder1VersionsInfo.Mode(), folder1Perms)
  218. }
  219. folder2VersionsInfo, err = os.Stat(folder2VersionsPath)
  220. if err != nil {
  221. t.Fatal(err)
  222. }
  223. if folder2VersionsInfo.Mode().Perm() != folder2Perms {
  224. t.Errorf("földer2 permissions %v, want %v", folder2VersionsInfo.Mode(), folder2Perms)
  225. }
  226. }