mtimefs_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 http://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "errors"
  9. "io/ioutil"
  10. "os"
  11. "testing"
  12. "time"
  13. "github.com/syncthing/syncthing/lib/osutil"
  14. )
  15. func TestMtimeFS(t *testing.T) {
  16. osutil.RemoveAll("testdata")
  17. defer osutil.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(make(mapStore))
  25. // Do one Chtimes call that will go through to the normal filesystem
  26. osChtimes = 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. osChtimes = 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. osChtimes = 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 successfull, 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. // The mapStore is a simple database
  70. type mapStore map[string][]byte
  71. func (s mapStore) PutBytes(key string, data []byte) {
  72. s[key] = data
  73. }
  74. func (s mapStore) Bytes(key string) (data []byte, ok bool) {
  75. data, ok = s[key]
  76. return
  77. }
  78. func (s mapStore) Delete(key string) {
  79. delete(s, key)
  80. }
  81. // failChtimes does nothing, and fails
  82. func failChtimes(name string, mtime, atime time.Time) error {
  83. return errors.New("no")
  84. }
  85. // evilChtimes will set an mtime that's 300 days in the future of what was
  86. // asked for, and truncate the time to the closest hour.
  87. func evilChtimes(name string, mtime, atime time.Time) error {
  88. return os.Chtimes(name, mtime.Add(300*time.Hour).Truncate(time.Hour), atime.Add(300*time.Hour).Truncate(time.Hour))
  89. }