github.com/jingleWang/moby@v1.13.1/pkg/system/chtimes_windows_test.go (about)

     1  // +build windows
     2  
     3  package system
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  // TestChtimesWindows tests Chtimes access time on a tempfile on Windows
    13  func TestChtimesWindows(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  	aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    31  	if aTime != unixEpochTime {
    32  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
    33  	}
    34  
    35  	// Test aTime before Unix Epoch and mTime set to Unix Epoch
    36  	Chtimes(file, beforeUnixEpochTime, unixEpochTime)
    37  
    38  	f, err = os.Stat(file)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    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  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    57  	if aTime != unixEpochTime {
    58  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
    59  	}
    60  
    61  	// Test both aTime and mTime set to after Unix Epoch (valid time)
    62  	Chtimes(file, afterUnixEpochTime, afterUnixEpochTime)
    63  
    64  	f, err = os.Stat(file)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    70  	if aTime != afterUnixEpochTime {
    71  		t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime)
    72  	}
    73  
    74  	// Test both aTime and mTime set to Unix max time
    75  	Chtimes(file, unixMaxTime, unixMaxTime)
    76  
    77  	f, err = os.Stat(file)
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    83  	if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) {
    84  		t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second))
    85  	}
    86  }