github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/pkg/system/chtimes_windows_test.go (about) 1 //go:build windows 2 3 package system // import "github.com/Prakhar-Agarwal-byte/moby/pkg/system" 4 5 import ( 6 "os" 7 "path/filepath" 8 "syscall" 9 "testing" 10 "time" 11 ) 12 13 // TestChtimesATimeWindows tests Chtimes access time on a tempfile on Windows. 14 func TestChtimesATimeWindows(t *testing.T) { 15 file := filepath.Join(t.TempDir(), "exist") 16 if err := os.WriteFile(file, []byte("hello"), 0o644); err != nil { 17 t.Fatal(err) 18 } 19 20 beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second) 21 afterUnixEpochTime := unixEpochTime.Add(100 * time.Second) 22 23 // Test both aTime and mTime set to Unix Epoch 24 t.Run("both aTime and mTime set to Unix Epoch", func(t *testing.T) { 25 if err := Chtimes(file, unixEpochTime, unixEpochTime); err != nil { 26 t.Error(err) 27 } 28 29 f, err := os.Stat(file) 30 if err != nil { 31 t.Fatal(err) 32 } 33 34 aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) 35 if aTime != unixEpochTime { 36 t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) 37 } 38 }) 39 40 // Test aTime before Unix Epoch and mTime set to Unix Epoch 41 t.Run("aTime before Unix Epoch and mTime set to Unix Epoch", func(t *testing.T) { 42 if err := Chtimes(file, beforeUnixEpochTime, unixEpochTime); err != nil { 43 t.Error(err) 44 } 45 46 f, err := os.Stat(file) 47 if err != nil { 48 t.Fatal(err) 49 } 50 51 aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) 52 if aTime != unixEpochTime { 53 t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) 54 } 55 }) 56 57 // Test aTime set to Unix Epoch and mTime before Unix Epoch 58 t.Run("aTime set to Unix Epoch and mTime before Unix Epoch", func(t *testing.T) { 59 if err := Chtimes(file, unixEpochTime, beforeUnixEpochTime); err != nil { 60 t.Error(err) 61 } 62 63 f, err := os.Stat(file) 64 if err != nil { 65 t.Fatal(err) 66 } 67 68 aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) 69 if aTime != unixEpochTime { 70 t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) 71 } 72 }) 73 74 // Test both aTime and mTime set to after Unix Epoch (valid time) 75 t.Run("both aTime and mTime set to after Unix Epoch (valid time)", func(t *testing.T) { 76 if err := Chtimes(file, afterUnixEpochTime, afterUnixEpochTime); err != nil { 77 t.Error(err) 78 } 79 80 f, err := os.Stat(file) 81 if err != nil { 82 t.Fatal(err) 83 } 84 85 aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) 86 if aTime != afterUnixEpochTime { 87 t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime) 88 } 89 }) 90 91 // Test both aTime and mTime set to Unix max time 92 t.Run("both aTime and mTime set to Unix max time", func(t *testing.T) { 93 if err := Chtimes(file, unixMaxTime, unixMaxTime); err != nil { 94 t.Error(err) 95 } 96 97 f, err := os.Stat(file) 98 if err != nil { 99 t.Fatal(err) 100 } 101 102 aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) 103 if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) { 104 t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second)) 105 } 106 }) 107 }