github.com/a4a881d4/docker@v1.9.0-rc2/pkg/system/chtimes_unix_test.go (about) 1 // +build linux freebsd 2 3 package system 4 5 import ( 6 "os" 7 "syscall" 8 "testing" 9 "time" 10 ) 11 12 // TestChtimes tests Chtimes access time on a tempfile on Linux 13 func TestChtimesLinux(t *testing.T) { 14 file, dir := prepareTempFile(t) 15 defer os.RemoveAll(dir) 16 17 beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second) 18 unixEpochTime := time.Unix(0, 0) 19 afterUnixEpochTime := time.Unix(100, 0) 20 // The max Unix time is 33 bits set 21 unixMaxTime := unixEpochTime.Add((1<<33 - 1) * time.Second) 22 afterUnixMaxTime := unixMaxTime.Add(100 * time.Second) 23 24 // Test both aTime and mTime set to Unix Epoch 25 Chtimes(file, unixEpochTime, unixEpochTime) 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(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) 34 if aTime != unixEpochTime { 35 t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) 36 } 37 38 // Test aTime before Unix Epoch and mTime set to Unix Epoch 39 Chtimes(file, beforeUnixEpochTime, unixEpochTime) 40 41 f, err = os.Stat(file) 42 if err != nil { 43 t.Fatal(err) 44 } 45 46 stat = f.Sys().(*syscall.Stat_t) 47 aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) 48 if aTime != unixEpochTime { 49 t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) 50 } 51 52 // Test aTime set to Unix Epoch and mTime before Unix Epoch 53 Chtimes(file, unixEpochTime, beforeUnixEpochTime) 54 55 f, err = os.Stat(file) 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 stat = f.Sys().(*syscall.Stat_t) 61 aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) 62 if aTime != unixEpochTime { 63 t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) 64 } 65 66 // Test both aTime and mTime set to after Unix Epoch (valid time) 67 Chtimes(file, afterUnixEpochTime, afterUnixEpochTime) 68 69 f, err = os.Stat(file) 70 if err != nil { 71 t.Fatal(err) 72 } 73 74 stat = f.Sys().(*syscall.Stat_t) 75 aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) 76 if aTime != afterUnixEpochTime { 77 t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime) 78 } 79 80 // Test both aTime and mTime set to Unix max time 81 Chtimes(file, unixMaxTime, unixMaxTime) 82 83 f, err = os.Stat(file) 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 stat = f.Sys().(*syscall.Stat_t) 89 aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) 90 if aTime != unixMaxTime { 91 t.Fatalf("Expected: %s, got: %s", unixMaxTime, aTime) 92 } 93 94 // Test aTime after Unix max time and mTime set to Unix max time 95 Chtimes(file, afterUnixMaxTime, unixMaxTime) 96 97 f, err = os.Stat(file) 98 if err != nil { 99 t.Fatal(err) 100 } 101 102 stat = f.Sys().(*syscall.Stat_t) 103 aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) 104 if aTime != unixEpochTime { 105 t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) 106 } 107 108 // Test aTime set to Unix Epoch and mTime before Unix Epoch 109 Chtimes(file, unixMaxTime, afterUnixMaxTime) 110 111 f, err = os.Stat(file) 112 if err != nil { 113 t.Fatal(err) 114 } 115 116 stat = f.Sys().(*syscall.Stat_t) 117 aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) 118 if aTime != unixMaxTime { 119 t.Fatalf("Expected: %s, got: %s", unixMaxTime, aTime) 120 } 121 }