2021-08-23 13:14:53 +00:00
|
|
|
//go:build windows
|
2015-10-01 17:45:32 +00:00
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package system // import "github.com/docker/docker/pkg/system"
|
2015-10-01 17:45:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-10-07 13:44:50 +00:00
|
|
|
"path/filepath"
|
2015-10-01 17:45:32 +00:00
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-10-07 13:55:45 +00:00
|
|
|
// TestChtimesATimeWindows tests Chtimes access time on a tempfile on Windows.
|
|
|
|
func TestChtimesATimeWindows(t *testing.T) {
|
2022-10-07 13:44:50 +00:00
|
|
|
file := filepath.Join(t.TempDir(), "exist")
|
|
|
|
if err := os.WriteFile(file, []byte("hello"), 0o644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-01 17:45:32 +00:00
|
|
|
|
2022-10-07 12:06:47 +00:00
|
|
|
beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second)
|
|
|
|
afterUnixEpochTime := unixEpochTime.Add(100 * time.Second)
|
2015-10-01 17:45:32 +00:00
|
|
|
|
|
|
|
// Test both aTime and mTime set to Unix Epoch
|
2022-10-07 14:17:11 +00:00
|
|
|
t.Run("both aTime and mTime set to Unix Epoch", func(t *testing.T) {
|
|
|
|
if err := Chtimes(file, unixEpochTime, unixEpochTime); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
|
|
|
|
if aTime != unixEpochTime {
|
|
|
|
t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
|
|
|
|
}
|
|
|
|
})
|
2015-10-01 17:45:32 +00:00
|
|
|
|
|
|
|
// Test aTime before Unix Epoch and mTime set to Unix Epoch
|
2022-10-07 14:17:11 +00:00
|
|
|
t.Run("aTime before Unix Epoch and mTime set to Unix Epoch", func(t *testing.T) {
|
|
|
|
if err := Chtimes(file, beforeUnixEpochTime, unixEpochTime); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
|
|
|
|
if aTime != unixEpochTime {
|
|
|
|
t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
|
|
|
|
}
|
|
|
|
})
|
2015-10-01 17:45:32 +00:00
|
|
|
|
|
|
|
// Test aTime set to Unix Epoch and mTime before Unix Epoch
|
2022-10-07 14:17:11 +00:00
|
|
|
t.Run("aTime set to Unix Epoch and mTime before Unix Epoch", func(t *testing.T) {
|
|
|
|
if err := Chtimes(file, unixEpochTime, beforeUnixEpochTime); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
|
|
|
|
if aTime != unixEpochTime {
|
|
|
|
t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
|
|
|
|
}
|
|
|
|
})
|
2015-10-01 17:45:32 +00:00
|
|
|
|
|
|
|
// Test both aTime and mTime set to after Unix Epoch (valid time)
|
2022-10-07 14:17:11 +00:00
|
|
|
t.Run("both aTime and mTime set to after Unix Epoch (valid time)", func(t *testing.T) {
|
|
|
|
if err := Chtimes(file, afterUnixEpochTime, afterUnixEpochTime); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
|
|
|
|
if aTime != afterUnixEpochTime {
|
|
|
|
t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime)
|
|
|
|
}
|
|
|
|
})
|
2015-10-01 17:45:32 +00:00
|
|
|
|
|
|
|
// Test both aTime and mTime set to Unix max time
|
2022-10-07 14:17:11 +00:00
|
|
|
t.Run("both aTime and mTime set to Unix max time", func(t *testing.T) {
|
|
|
|
if err := Chtimes(file, unixMaxTime, unixMaxTime); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
|
|
|
|
if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) {
|
|
|
|
t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second))
|
|
|
|
}
|
|
|
|
})
|
2015-10-01 17:45:32 +00:00
|
|
|
}
|