chtimes_windows_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // +build windows
  2. package system
  3. import (
  4. "os"
  5. "syscall"
  6. "testing"
  7. "time"
  8. )
  9. // TestChtimesWindows tests Chtimes access time on a tempfile on Windows
  10. func TestChtimesWindows(t *testing.T) {
  11. file, dir := prepareTempFile(t)
  12. defer os.RemoveAll(dir)
  13. beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second)
  14. unixEpochTime := time.Unix(0, 0)
  15. afterUnixEpochTime := time.Unix(100, 0)
  16. unixMaxTime := maxTime
  17. // Test both aTime and mTime set to Unix Epoch
  18. Chtimes(file, unixEpochTime, unixEpochTime)
  19. f, err := os.Stat(file)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
  24. if aTime != unixEpochTime {
  25. t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
  26. }
  27. // Test aTime before Unix Epoch and mTime set to Unix Epoch
  28. Chtimes(file, beforeUnixEpochTime, unixEpochTime)
  29. f, err = os.Stat(file)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
  34. if aTime != unixEpochTime {
  35. t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
  36. }
  37. // Test aTime set to Unix Epoch and mTime before Unix Epoch
  38. Chtimes(file, unixEpochTime, beforeUnixEpochTime)
  39. f, err = os.Stat(file)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
  44. if aTime != unixEpochTime {
  45. t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
  46. }
  47. // Test both aTime and mTime set to after Unix Epoch (valid time)
  48. Chtimes(file, afterUnixEpochTime, afterUnixEpochTime)
  49. f, err = os.Stat(file)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
  54. if aTime != afterUnixEpochTime {
  55. t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime)
  56. }
  57. // Test both aTime and mTime set to Unix max time
  58. Chtimes(file, unixMaxTime, unixMaxTime)
  59. f, err = os.Stat(file)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
  64. if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) {
  65. t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second))
  66. }
  67. }