chtimes_linux_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package system // import "github.com/docker/docker/pkg/system"
  2. import (
  3. "os"
  4. "path/filepath"
  5. "syscall"
  6. "testing"
  7. "time"
  8. )
  9. // TestChtimesATime tests Chtimes access time on a tempfile.
  10. func TestChtimesATime(t *testing.T) {
  11. file := filepath.Join(t.TempDir(), "exist")
  12. if err := os.WriteFile(file, []byte("hello"), 0o644); err != nil {
  13. t.Fatal(err)
  14. }
  15. beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second)
  16. afterUnixEpochTime := unixEpochTime.Add(100 * time.Second)
  17. // Test both aTime and mTime set to Unix Epoch
  18. t.Run("both aTime and mTime set to Unix Epoch", func(t *testing.T) {
  19. if err := Chtimes(file, unixEpochTime, unixEpochTime); err != nil {
  20. t.Error(err)
  21. }
  22. f, err := os.Stat(file)
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. stat := f.Sys().(*syscall.Stat_t)
  27. aTime := time.Unix(stat.Atim.Unix())
  28. if aTime != unixEpochTime {
  29. t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
  30. }
  31. })
  32. // Test aTime before Unix Epoch and mTime set to Unix Epoch
  33. t.Run("aTime before Unix Epoch and mTime set to Unix Epoch", func(t *testing.T) {
  34. if err := Chtimes(file, beforeUnixEpochTime, unixEpochTime); err != nil {
  35. t.Error(err)
  36. }
  37. f, err := os.Stat(file)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. stat := f.Sys().(*syscall.Stat_t)
  42. aTime := time.Unix(stat.Atim.Unix())
  43. if aTime != unixEpochTime {
  44. t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
  45. }
  46. })
  47. // Test aTime set to Unix Epoch and mTime before Unix Epoch
  48. t.Run("aTime set to Unix Epoch and mTime before Unix Epoch", func(t *testing.T) {
  49. if err := Chtimes(file, unixEpochTime, beforeUnixEpochTime); err != nil {
  50. t.Error(err)
  51. }
  52. f, err := os.Stat(file)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. stat := f.Sys().(*syscall.Stat_t)
  57. aTime := time.Unix(stat.Atim.Unix())
  58. if aTime != unixEpochTime {
  59. t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
  60. }
  61. })
  62. // Test both aTime and mTime set to after Unix Epoch (valid time)
  63. t.Run("both aTime and mTime set to after Unix Epoch (valid time)", func(t *testing.T) {
  64. if err := Chtimes(file, afterUnixEpochTime, afterUnixEpochTime); err != nil {
  65. t.Error(err)
  66. }
  67. f, err := os.Stat(file)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. stat := f.Sys().(*syscall.Stat_t)
  72. aTime := time.Unix(stat.Atim.Unix())
  73. if aTime != afterUnixEpochTime {
  74. t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime)
  75. }
  76. })
  77. // Test both aTime and mTime set to Unix max time
  78. t.Run("both aTime and mTime set to Unix max time", func(t *testing.T) {
  79. if err := Chtimes(file, unixMaxTime, unixMaxTime); err != nil {
  80. t.Error(err)
  81. }
  82. f, err := os.Stat(file)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. stat := f.Sys().(*syscall.Stat_t)
  87. aTime := time.Unix(stat.Atim.Unix())
  88. if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) {
  89. t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second))
  90. }
  91. })
  92. }