chtimes_unix_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // +build !windows
  2. package system // import "github.com/docker/docker/pkg/system"
  3. import (
  4. "os"
  5. "syscall"
  6. "testing"
  7. "time"
  8. )
  9. // TestChtimesLinux tests Chtimes access time on a tempfile on Linux
  10. func TestChtimesLinux(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. stat := f.Sys().(*syscall.Stat_t)
  24. aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
  25. if aTime != unixEpochTime {
  26. t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
  27. }
  28. // Test aTime before Unix Epoch and mTime set to Unix Epoch
  29. Chtimes(file, beforeUnixEpochTime, unixEpochTime)
  30. f, err = os.Stat(file)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. stat = f.Sys().(*syscall.Stat_t)
  35. aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
  36. if aTime != unixEpochTime {
  37. t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
  38. }
  39. // Test aTime set to Unix Epoch and mTime before Unix Epoch
  40. Chtimes(file, unixEpochTime, beforeUnixEpochTime)
  41. f, err = os.Stat(file)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. stat = f.Sys().(*syscall.Stat_t)
  46. aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
  47. if aTime != unixEpochTime {
  48. t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
  49. }
  50. // Test both aTime and mTime set to after Unix Epoch (valid time)
  51. Chtimes(file, afterUnixEpochTime, afterUnixEpochTime)
  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(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
  58. if aTime != afterUnixEpochTime {
  59. t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime)
  60. }
  61. // Test both aTime and mTime set to Unix max time
  62. Chtimes(file, unixMaxTime, unixMaxTime)
  63. f, err = os.Stat(file)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. stat = f.Sys().(*syscall.Stat_t)
  68. aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
  69. if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) {
  70. t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second))
  71. }
  72. }