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