mtimefs_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. package fs
  7. import (
  8. "errors"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "testing"
  13. "time"
  14. )
  15. func TestMtimeFS(t *testing.T) {
  16. os.RemoveAll("testdata")
  17. defer os.RemoveAll("testdata")
  18. os.Mkdir("testdata", 0755)
  19. os.WriteFile("testdata/exists0", []byte("hello"), 0644)
  20. os.WriteFile("testdata/exists1", []byte("hello"), 0644)
  21. os.WriteFile("testdata/exists2", []byte("hello"), 0644)
  22. // a random time with nanosecond precision
  23. testTime := time.Unix(1234567890, 123456789)
  24. mtimefs := newMtimeFS(".", make(mapStore))
  25. // Do one Chtimes call that will go through to the normal filesystem
  26. mtimefs.chtimes = os.Chtimes
  27. if err := mtimefs.Chtimes("testdata/exists0", testTime, testTime); err != nil {
  28. t.Error("Should not have failed:", err)
  29. }
  30. // Do one call that gets an error back from the underlying Chtimes
  31. mtimefs.chtimes = failChtimes
  32. if err := mtimefs.Chtimes("testdata/exists1", testTime, testTime); err != nil {
  33. t.Error("Should not have failed:", err)
  34. }
  35. // Do one call that gets struck by an exceptionally evil Chtimes
  36. mtimefs.chtimes = evilChtimes
  37. if err := mtimefs.Chtimes("testdata/exists2", testTime, testTime); err != nil {
  38. t.Error("Should not have failed:", err)
  39. }
  40. // All of the calls were successful, so an Lstat on them should return
  41. // the test timestamp.
  42. for _, file := range []string{"testdata/exists0", "testdata/exists1", "testdata/exists2"} {
  43. if info, err := mtimefs.Lstat(file); err != nil {
  44. t.Error("Lstat shouldn't fail:", err)
  45. } else if !info.ModTime().Equal(testTime) {
  46. t.Errorf("Time mismatch; %v != expected %v", info.ModTime(), testTime)
  47. }
  48. }
  49. // The two last files should certainly not have the correct timestamp
  50. // when looking directly on disk though.
  51. for _, file := range []string{"testdata/exists1", "testdata/exists2"} {
  52. if info, err := os.Lstat(file); err != nil {
  53. t.Error("Lstat shouldn't fail:", err)
  54. } else if info.ModTime().Equal(testTime) {
  55. t.Errorf("Unexpected time match; %v == %v", info.ModTime(), testTime)
  56. }
  57. }
  58. // Changing the timestamp on disk should be reflected in a new Lstat
  59. // call. Choose a time that is likely to be able to be on all reasonable
  60. // filesystems.
  61. testTime = time.Now().Add(5 * time.Hour).Truncate(time.Minute)
  62. os.Chtimes("testdata/exists0", testTime, testTime)
  63. if info, err := mtimefs.Lstat("testdata/exists0"); err != nil {
  64. t.Error("Lstat shouldn't fail:", err)
  65. } else if !info.ModTime().Equal(testTime) {
  66. t.Errorf("Time mismatch; %v != expected %v", info.ModTime(), testTime)
  67. }
  68. }
  69. func TestMtimeFSWalk(t *testing.T) {
  70. dir := t.TempDir()
  71. mtimefs, walkFs := newMtimeFSWithWalk(dir, make(mapStore))
  72. underlying := mtimefs.Filesystem
  73. mtimefs.chtimes = failChtimes
  74. if err := os.WriteFile(filepath.Join(dir, "file"), []byte("hello"), 0644); err != nil {
  75. t.Fatal(err)
  76. }
  77. oldStat, err := mtimefs.Lstat("file")
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. newTime := time.Now().Add(-2 * time.Hour)
  82. if err := mtimefs.Chtimes("file", newTime, newTime); err != nil {
  83. t.Fatal(err)
  84. }
  85. if newStat, err := mtimefs.Lstat("file"); err != nil {
  86. t.Fatal(err)
  87. } else if !newStat.ModTime().Equal(newTime) {
  88. t.Errorf("expected time %v, lstat time %v", newTime, newStat.ModTime())
  89. }
  90. if underlyingStat, err := underlying.Lstat("file"); err != nil {
  91. t.Fatal(err)
  92. } else if !underlyingStat.ModTime().Equal(oldStat.ModTime()) {
  93. t.Errorf("expected time %v, lstat time %v", oldStat.ModTime(), underlyingStat.ModTime())
  94. }
  95. found := false
  96. _ = walkFs.Walk("", func(path string, info FileInfo, err error) error {
  97. if path == "file" {
  98. found = true
  99. if !info.ModTime().Equal(newTime) {
  100. t.Errorf("expected time %v, lstat time %v", newTime, info.ModTime())
  101. }
  102. }
  103. return nil
  104. })
  105. if !found {
  106. t.Error("did not find")
  107. }
  108. }
  109. func TestMtimeFSOpen(t *testing.T) {
  110. dir := t.TempDir()
  111. mtimefs := newMtimeFS(dir, make(mapStore))
  112. underlying := mtimefs.Filesystem
  113. mtimefs.chtimes = failChtimes
  114. if err := os.WriteFile(filepath.Join(dir, "file"), []byte("hello"), 0644); err != nil {
  115. t.Fatal(err)
  116. }
  117. oldStat, err := mtimefs.Lstat("file")
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. newTime := time.Now().Add(-2 * time.Hour)
  122. if err := mtimefs.Chtimes("file", newTime, newTime); err != nil {
  123. t.Fatal(err)
  124. }
  125. if newStat, err := mtimefs.Lstat("file"); err != nil {
  126. t.Fatal(err)
  127. } else if !newStat.ModTime().Equal(newTime) {
  128. t.Errorf("expected time %v, lstat time %v", newTime, newStat.ModTime())
  129. }
  130. if underlyingStat, err := underlying.Lstat("file"); err != nil {
  131. t.Fatal(err)
  132. } else if !underlyingStat.ModTime().Equal(oldStat.ModTime()) {
  133. t.Errorf("expected time %v, lstat time %v", oldStat.ModTime(), underlyingStat.ModTime())
  134. }
  135. fd, err := mtimefs.Open("file")
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. defer fd.Close()
  140. info, err := fd.Stat()
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. if !info.ModTime().Equal(newTime) {
  145. t.Errorf("expected time %v, lstat time %v", newTime, info.ModTime())
  146. }
  147. }
  148. func TestMtimeFSInsensitive(t *testing.T) {
  149. switch runtime.GOOS {
  150. case "darwin", "windows":
  151. // blatantly assume file systems here are case insensitive. Might be
  152. // a spurious failure on oddly configured systems.
  153. default:
  154. t.Skip("need case insensitive FS")
  155. }
  156. theTest := func(t *testing.T, fs *mtimeFS, shouldSucceed bool) {
  157. os.RemoveAll("testdata")
  158. defer os.RemoveAll("testdata")
  159. os.Mkdir("testdata", 0755)
  160. os.WriteFile("testdata/FiLe", []byte("hello"), 0644)
  161. // a random time with nanosecond precision
  162. testTime := time.Unix(1234567890, 123456789)
  163. // Do one call that gets struck by an exceptionally evil Chtimes, with a
  164. // different case from what is on disk.
  165. fs.chtimes = evilChtimes
  166. if err := fs.Chtimes("testdata/fIlE", testTime, testTime); err != nil {
  167. t.Error("Should not have failed:", err)
  168. }
  169. // Check that we get back the mtime we set, if we were supposed to succed.
  170. info, err := fs.Lstat("testdata/FILE")
  171. if err != nil {
  172. t.Error("Lstat shouldn't fail:", err)
  173. } else if info.ModTime().Equal(testTime) != shouldSucceed {
  174. t.Errorf("Time mismatch; got %v, comparison %v, expected equal=%v", info.ModTime(), testTime, shouldSucceed)
  175. }
  176. }
  177. // The test should fail with a case sensitive mtimefs
  178. t.Run("with case sensitive mtimefs", func(t *testing.T) {
  179. theTest(t, newMtimeFS(".", make(mapStore)), false)
  180. })
  181. // And succeed with a case insensitive one.
  182. t.Run("with case insensitive mtimefs", func(t *testing.T) {
  183. theTest(t, newMtimeFS(".", make(mapStore), WithCaseInsensitivity(true)), true)
  184. })
  185. }
  186. // The mapStore is a simple database
  187. type mapStore map[string][]byte
  188. func (s mapStore) PutBytes(key string, data []byte) error {
  189. s[key] = data
  190. return nil
  191. }
  192. func (s mapStore) Bytes(key string) (data []byte, ok bool, err error) {
  193. data, ok = s[key]
  194. return
  195. }
  196. func (s mapStore) Delete(key string) error {
  197. delete(s, key)
  198. return nil
  199. }
  200. // failChtimes does nothing, and fails
  201. func failChtimes(name string, mtime, atime time.Time) error {
  202. return errors.New("no")
  203. }
  204. // evilChtimes will set an mtime that's 300 days in the future of what was
  205. // asked for, and truncate the time to the closest hour.
  206. func evilChtimes(name string, mtime, atime time.Time) error {
  207. return os.Chtimes(name, mtime.Add(300*time.Hour).Truncate(time.Hour), atime.Add(300*time.Hour).Truncate(time.Hour))
  208. }
  209. func newMtimeFS(path string, db database, options ...MtimeFSOption) *mtimeFS {
  210. mtimefs, _ := newMtimeFSWithWalk(path, db, options...)
  211. return mtimefs
  212. }
  213. func newMtimeFSWithWalk(path string, db database, options ...MtimeFSOption) (*mtimeFS, *walkFilesystem) {
  214. wfs := NewFilesystem(FilesystemTypeBasic, path, NewMtimeOption(db, options...)).(*walkFilesystem)
  215. return wfs.Filesystem.(*mtimeFS), wfs
  216. }