chtimes_linux_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package system // import "github.com/docker/docker/pkg/system"
  2. import (
  3. "os"
  4. "syscall"
  5. "testing"
  6. "time"
  7. )
  8. // TestChtimesLinux tests Chtimes access time on a tempfile on Linux
  9. func TestChtimesLinux(t *testing.T) {
  10. file, dir := prepareTempFile(t)
  11. defer os.RemoveAll(dir)
  12. beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second)
  13. unixEpochTime := time.Unix(0, 0)
  14. afterUnixEpochTime := time.Unix(100, 0)
  15. unixMaxTime := maxTime
  16. // Test both aTime and mTime set to Unix Epoch
  17. Chtimes(file, unixEpochTime, unixEpochTime)
  18. f, err := os.Stat(file)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. stat := f.Sys().(*syscall.Stat_t)
  23. aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
  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. stat = f.Sys().(*syscall.Stat_t)
  34. aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
  35. if aTime != unixEpochTime {
  36. t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
  37. }
  38. // Test aTime set to Unix Epoch and mTime before Unix Epoch
  39. Chtimes(file, unixEpochTime, beforeUnixEpochTime)
  40. f, err = os.Stat(file)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. stat = f.Sys().(*syscall.Stat_t)
  45. aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
  46. if aTime != unixEpochTime {
  47. t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
  48. }
  49. // Test both aTime and mTime set to after Unix Epoch (valid time)
  50. Chtimes(file, afterUnixEpochTime, afterUnixEpochTime)
  51. f, err = os.Stat(file)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. stat = f.Sys().(*syscall.Stat_t)
  56. aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
  57. if aTime != afterUnixEpochTime {
  58. t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime)
  59. }
  60. // Test both aTime and mTime set to Unix max time
  61. Chtimes(file, unixMaxTime, unixMaxTime)
  62. f, err = os.Stat(file)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. stat = f.Sys().(*syscall.Stat_t)
  67. aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
  68. if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) {
  69. t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second))
  70. }
  71. }