folder_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (C) 2018 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 model
  7. import (
  8. "path/filepath"
  9. "testing"
  10. "github.com/d4l3k/messagediff"
  11. "github.com/syncthing/syncthing/lib/build"
  12. "github.com/syncthing/syncthing/lib/config"
  13. "github.com/syncthing/syncthing/lib/fs"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. "github.com/syncthing/syncthing/lib/rand"
  16. )
  17. type unifySubsCase struct {
  18. in []string // input to unifySubs
  19. exists []string // paths that exist in the database
  20. out []string // expected output
  21. }
  22. func unifySubsCases() []unifySubsCase {
  23. cases := []unifySubsCase{
  24. {
  25. // 0. trailing slashes are cleaned, known paths are just passed on
  26. []string{"foo/", "bar//"},
  27. []string{"foo", "bar"},
  28. []string{"bar", "foo"}, // the output is sorted
  29. },
  30. {
  31. // 1. "foo/bar" gets trimmed as it's covered by foo
  32. []string{"foo", "bar/", "foo/bar/"},
  33. []string{"foo", "bar"},
  34. []string{"bar", "foo"},
  35. },
  36. {
  37. // 2. "" gets simplified to the empty list; ie scan all
  38. []string{"foo", ""},
  39. []string{"foo"},
  40. nil,
  41. },
  42. {
  43. // 3. "foo/bar" is unknown, but it's kept
  44. // because its parent is known
  45. []string{"foo/bar"},
  46. []string{"foo"},
  47. []string{"foo/bar"},
  48. },
  49. {
  50. // 4. two independent known paths, both are kept
  51. // "usr/lib" is not a prefix of "usr/libexec"
  52. []string{"usr/lib", "usr/libexec"},
  53. []string{"usr", "usr/lib", "usr/libexec"},
  54. []string{"usr/lib", "usr/libexec"},
  55. },
  56. {
  57. // 5. "usr/lib" is a prefix of "usr/lib/exec"
  58. []string{"usr/lib", "usr/lib/exec"},
  59. []string{"usr", "usr/lib", "usr/libexec"},
  60. []string{"usr/lib"},
  61. },
  62. {
  63. // 6. .stignore and .stfolder are special and are passed on
  64. // verbatim even though they are unknown
  65. []string{config.DefaultMarkerName, ".stignore"},
  66. []string{},
  67. []string{config.DefaultMarkerName, ".stignore"},
  68. },
  69. {
  70. // 7. but the presence of something else unknown forces an actual
  71. // scan
  72. []string{config.DefaultMarkerName, ".stignore", "foo/bar"},
  73. []string{},
  74. []string{config.DefaultMarkerName, ".stignore", "foo"},
  75. },
  76. {
  77. // 8. explicit request to scan all
  78. nil,
  79. []string{"foo"},
  80. nil,
  81. },
  82. {
  83. // 9. empty list of subs
  84. []string{},
  85. []string{"foo"},
  86. nil,
  87. },
  88. {
  89. // 10. absolute path
  90. []string{"/foo"},
  91. []string{"foo"},
  92. []string{"foo"},
  93. },
  94. }
  95. if build.IsWindows {
  96. // Fixup path separators
  97. for i := range cases {
  98. for j, p := range cases[i].in {
  99. cases[i].in[j] = filepath.FromSlash(p)
  100. }
  101. for j, p := range cases[i].exists {
  102. cases[i].exists[j] = filepath.FromSlash(p)
  103. }
  104. for j, p := range cases[i].out {
  105. cases[i].out[j] = filepath.FromSlash(p)
  106. }
  107. }
  108. }
  109. return cases
  110. }
  111. func unifyExists(f string, tc unifySubsCase) bool {
  112. for _, e := range tc.exists {
  113. if f == e {
  114. return true
  115. }
  116. }
  117. return false
  118. }
  119. func TestUnifySubs(t *testing.T) {
  120. cases := unifySubsCases()
  121. for i, tc := range cases {
  122. exists := func(f string) bool {
  123. return unifyExists(f, tc)
  124. }
  125. out := unifySubs(tc.in, exists)
  126. if diff, equal := messagediff.PrettyDiff(tc.out, out); !equal {
  127. t.Errorf("Case %d failed; got %v, expected %v, diff:\n%s", i, out, tc.out, diff)
  128. }
  129. }
  130. }
  131. func BenchmarkUnifySubs(b *testing.B) {
  132. cases := unifySubsCases()
  133. b.ReportAllocs()
  134. b.ResetTimer()
  135. for i := 0; i < b.N; i++ {
  136. for _, tc := range cases {
  137. exists := func(f string) bool {
  138. return unifyExists(f, tc)
  139. }
  140. unifySubs(tc.in, exists)
  141. }
  142. }
  143. }
  144. func TestSetPlatformData(t *testing.T) {
  145. // Checks that setPlatformData runs without error when applied to a temp
  146. // file, named differently than the given FileInfo.
  147. fs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32))
  148. if fd, err := fs.Create("file.tmp"); err != nil {
  149. t.Fatal(err)
  150. } else {
  151. fd.Close()
  152. }
  153. xattr := []protocol.Xattr{{Name: "user.foo", Value: []byte("bar")}}
  154. fi := &protocol.FileInfo{
  155. Name: "should be ignored",
  156. Permissions: 0o400,
  157. ModifiedS: 1234567890,
  158. Platform: protocol.PlatformData{
  159. Linux: &protocol.XattrData{Xattrs: xattr},
  160. Darwin: &protocol.XattrData{Xattrs: xattr},
  161. FreeBSD: &protocol.XattrData{Xattrs: xattr},
  162. NetBSD: &protocol.XattrData{Xattrs: xattr},
  163. },
  164. }
  165. // Minimum required to support setPlatformData
  166. sr := &sendReceiveFolder{
  167. folder: folder{
  168. FolderConfiguration: config.FolderConfiguration{
  169. SyncXattrs: true,
  170. },
  171. mtimefs: fs,
  172. },
  173. }
  174. if err := sr.setPlatformData(fi, "file.tmp"); err != nil {
  175. t.Error(err)
  176. }
  177. }