2018-02-05 16:05:59 -05:00
|
|
|
package system // import "github.com/docker/docker/pkg/system"
|
2015-10-01 13:45:32 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-07-26 08:04:11 -04:00
|
|
|
// TestChtimesLinux tests Chtimes access time on a tempfile on Linux
|
2015-10-01 13:45:32 -04:00
|
|
|
func TestChtimesLinux(t *testing.T) {
|
|
|
|
file, dir := prepareTempFile(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second)
|
|
|
|
unixEpochTime := time.Unix(0, 0)
|
|
|
|
afterUnixEpochTime := time.Unix(100, 0)
|
2015-11-03 17:49:50 -05:00
|
|
|
unixMaxTime := maxTime
|
2015-10-01 13:45:32 -04:00
|
|
|
|
|
|
|
// Test both aTime and mTime set to Unix Epoch
|
|
|
|
Chtimes(file, unixEpochTime, unixEpochTime)
|
|
|
|
|
|
|
|
f, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stat := f.Sys().(*syscall.Stat_t)
|
2019-08-06 20:38:51 -04:00
|
|
|
aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
|
2015-10-01 13:45:32 -04:00
|
|
|
if aTime != unixEpochTime {
|
|
|
|
t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test aTime before Unix Epoch and mTime set to Unix Epoch
|
|
|
|
Chtimes(file, beforeUnixEpochTime, unixEpochTime)
|
|
|
|
|
|
|
|
f, err = os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stat = f.Sys().(*syscall.Stat_t)
|
2019-08-06 20:38:51 -04:00
|
|
|
aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
|
2015-10-01 13:45:32 -04:00
|
|
|
if aTime != unixEpochTime {
|
|
|
|
t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test aTime set to Unix Epoch and mTime before Unix Epoch
|
|
|
|
Chtimes(file, unixEpochTime, beforeUnixEpochTime)
|
|
|
|
|
|
|
|
f, err = os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stat = f.Sys().(*syscall.Stat_t)
|
2019-08-06 20:38:51 -04:00
|
|
|
aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
|
2015-10-01 13:45:32 -04:00
|
|
|
if aTime != unixEpochTime {
|
|
|
|
t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test both aTime and mTime set to after Unix Epoch (valid time)
|
|
|
|
Chtimes(file, afterUnixEpochTime, afterUnixEpochTime)
|
|
|
|
|
|
|
|
f, err = os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stat = f.Sys().(*syscall.Stat_t)
|
2019-08-06 20:38:51 -04:00
|
|
|
aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
|
2015-10-01 13:45:32 -04:00
|
|
|
if aTime != afterUnixEpochTime {
|
|
|
|
t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test both aTime and mTime set to Unix max time
|
|
|
|
Chtimes(file, unixMaxTime, unixMaxTime)
|
|
|
|
|
|
|
|
f, err = os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stat = f.Sys().(*syscall.Stat_t)
|
2019-08-06 20:38:51 -04:00
|
|
|
aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) // nolint: unconvert
|
2015-11-03 17:49:50 -05:00
|
|
|
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 13:45:32 -04:00
|
|
|
}
|
|
|
|
}
|