github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/pkg/system/chtimes_linux_test.go (about) 1 package system // import "github.com/docker/docker/pkg/system" 2 3 import ( 4 "os" 5 "syscall" 6 "testing" 7 "time" 8 ) 9 10 // TestChtimesLinux tests Chtimes access time on a tempfile on Linux 11 func TestChtimesLinux(t *testing.T) { 12 file, dir := prepareTempFile(t) 13 defer os.RemoveAll(dir) 14 15 beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second) 16 unixEpochTime := time.Unix(0, 0) 17 afterUnixEpochTime := time.Unix(100, 0) 18 unixMaxTime := maxTime 19 20 // Test both aTime and mTime set to Unix Epoch 21 Chtimes(file, unixEpochTime, unixEpochTime) 22 23 f, err := os.Stat(file) 24 if err != nil { 25 t.Fatal(err) 26 } 27 28 stat := f.Sys().(*syscall.Stat_t) 29 aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) //nolint: unconvert 30 if aTime != unixEpochTime { 31 t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) 32 } 33 34 // Test aTime before Unix Epoch and mTime set to Unix Epoch 35 Chtimes(file, beforeUnixEpochTime, unixEpochTime) 36 37 f, err = os.Stat(file) 38 if err != nil { 39 t.Fatal(err) 40 } 41 42 stat = f.Sys().(*syscall.Stat_t) 43 aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) //nolint: unconvert 44 if aTime != unixEpochTime { 45 t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) 46 } 47 48 // Test aTime set to Unix Epoch and mTime before Unix Epoch 49 Chtimes(file, unixEpochTime, beforeUnixEpochTime) 50 51 f, err = os.Stat(file) 52 if err != nil { 53 t.Fatal(err) 54 } 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 != unixEpochTime { 59 t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) 60 } 61 62 // Test both aTime and mTime set to after Unix Epoch (valid time) 63 Chtimes(file, afterUnixEpochTime, afterUnixEpochTime) 64 65 f, err = os.Stat(file) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 stat = f.Sys().(*syscall.Stat_t) 71 aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) //nolint: unconvert 72 if aTime != afterUnixEpochTime { 73 t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime) 74 } 75 76 // Test both aTime and mTime set to Unix max time 77 Chtimes(file, unixMaxTime, unixMaxTime) 78 79 f, err = os.Stat(file) 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 stat = f.Sys().(*syscall.Stat_t) 85 aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) //nolint: unconvert 86 if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) { 87 t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second)) 88 } 89 }