github.com/ttys3/engine@v17.12.1-ce-rc2+incompatible/pkg/system/chtimes_unix_test.go (about)

     1  // +build !windows
     2  
     3  package system
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  // TestChtimesLinux 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  	unixMaxTime := maxTime
    21  
    22  	// Test both aTime and mTime set to Unix Epoch
    23  	Chtimes(file, unixEpochTime, unixEpochTime)
    24  
    25  	f, err := os.Stat(file)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	stat := f.Sys().(*syscall.Stat_t)
    31  	aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
    32  	if aTime != unixEpochTime {
    33  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
    34  	}
    35  
    36  	// Test aTime before Unix Epoch and mTime set to Unix Epoch
    37  	Chtimes(file, beforeUnixEpochTime, unixEpochTime)
    38  
    39  	f, err = os.Stat(file)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  
    44  	stat = f.Sys().(*syscall.Stat_t)
    45  	aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
    46  	if aTime != unixEpochTime {
    47  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
    48  	}
    49  
    50  	// Test aTime set to Unix Epoch and mTime before Unix Epoch
    51  	Chtimes(file, unixEpochTime, beforeUnixEpochTime)
    52  
    53  	f, err = os.Stat(file)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	stat = f.Sys().(*syscall.Stat_t)
    59  	aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
    60  	if aTime != unixEpochTime {
    61  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
    62  	}
    63  
    64  	// Test both aTime and mTime set to after Unix Epoch (valid time)
    65  	Chtimes(file, afterUnixEpochTime, afterUnixEpochTime)
    66  
    67  	f, err = os.Stat(file)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	stat = f.Sys().(*syscall.Stat_t)
    73  	aTime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
    74  	if aTime != afterUnixEpochTime {
    75  		t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime)
    76  	}
    77  
    78  	// Test both aTime and mTime set to Unix max time
    79  	Chtimes(file, unixMaxTime, unixMaxTime)
    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(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
    88  	if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) {
    89  		t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second))
    90  	}
    91  }