chtimes_windows_test.go 3.0 KB

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