mtimefs_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "io/ioutil"
  10. "os"
  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. ioutil.WriteFile("testdata/exists0", []byte("hello"), 0644)
  20. ioutil.WriteFile("testdata/exists1", []byte("hello"), 0644)
  21. ioutil.WriteFile("testdata/exists2", []byte("hello"), 0644)
  22. // a random time with nanosecond precision
  23. testTime := time.Unix(1234567890, 123456789)
  24. mtimefs := NewMtimeFS(newBasicFilesystem("."), 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 TestMtimeFSInsensitive(t *testing.T) {
  70. switch runtime.GOOS {
  71. case "darwin", "windows":
  72. // blatantly assume file systems here are case insensitive. Might be
  73. // a spurious failure on oddly configured systems.
  74. default:
  75. t.Skip("need case insensitive FS")
  76. }
  77. theTest := func(t *testing.T, fs *MtimeFS, shouldSucceed bool) {
  78. os.RemoveAll("testdata")
  79. defer os.RemoveAll("testdata")
  80. os.Mkdir("testdata", 0755)
  81. ioutil.WriteFile("testdata/FiLe", []byte("hello"), 0644)
  82. // a random time with nanosecond precision
  83. testTime := time.Unix(1234567890, 123456789)
  84. // Do one call that gets struck by an exceptionally evil Chtimes, with a
  85. // different case from what is on disk.
  86. fs.chtimes = evilChtimes
  87. if err := fs.Chtimes("testdata/fIlE", testTime, testTime); err != nil {
  88. t.Error("Should not have failed:", err)
  89. }
  90. // Check that we get back the mtime we set, if we were supposed to succed.
  91. info, err := fs.Lstat("testdata/FILE")
  92. if err != nil {
  93. t.Error("Lstat shouldn't fail:", err)
  94. } else if info.ModTime().Equal(testTime) != shouldSucceed {
  95. t.Errorf("Time mismatch; got %v, comparison %v, expected equal=%v", info.ModTime(), testTime, shouldSucceed)
  96. }
  97. }
  98. // The test should fail with a case sensitive mtimefs
  99. t.Run("with case sensitive mtimefs", func(t *testing.T) {
  100. theTest(t, NewMtimeFS(newBasicFilesystem("."), make(mapStore)), false)
  101. })
  102. // And succeed with a case insensitive one.
  103. t.Run("with case insensitive mtimefs", func(t *testing.T) {
  104. theTest(t, NewMtimeFS(newBasicFilesystem("."), make(mapStore), WithCaseInsensitivity(true)), true)
  105. })
  106. }
  107. // The mapStore is a simple database
  108. type mapStore map[string][]byte
  109. func (s mapStore) PutBytes(key string, data []byte) {
  110. s[key] = data
  111. }
  112. func (s mapStore) Bytes(key string) (data []byte, ok bool) {
  113. data, ok = s[key]
  114. return
  115. }
  116. func (s mapStore) Delete(key string) {
  117. delete(s, key)
  118. }
  119. // failChtimes does nothing, and fails
  120. func failChtimes(name string, mtime, atime time.Time) error {
  121. return errors.New("no")
  122. }
  123. // evilChtimes will set an mtime that's 300 days in the future of what was
  124. // asked for, and truncate the time to the closest hour.
  125. func evilChtimes(name string, mtime, atime time.Time) error {
  126. return os.Chtimes(name, mtime.Add(300*time.Hour).Truncate(time.Hour), atime.Add(300*time.Hour).Truncate(time.Hour))
  127. }