gopkg.in/docker/docker.v20@v20.10.27/pkg/system/chtimes_windows_test.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  package system // import "github.com/docker/docker/pkg/system"
     5  
     6  import (
     7  	"os"
     8  	"syscall"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  // TestChtimesWindows tests Chtimes access time on a tempfile on Windows
    14  func TestChtimesWindows(t *testing.T) {
    15  	file, dir := prepareTempFile(t)
    16  	defer os.RemoveAll(dir)
    17  
    18  	beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second)
    19  	unixEpochTime := time.Unix(0, 0)
    20  	afterUnixEpochTime := time.Unix(100, 0)
    21  	unixMaxTime := maxTime
    22  
    23  	// Test both aTime and mTime set to Unix Epoch
    24  	Chtimes(file, unixEpochTime, unixEpochTime)
    25  
    26  	f, err := os.Stat(file)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    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  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    45  	if aTime != unixEpochTime {
    46  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
    47  	}
    48  
    49  	// Test aTime set to Unix Epoch and mTime before Unix Epoch
    50  	Chtimes(file, unixEpochTime, beforeUnixEpochTime)
    51  
    52  	f, err = os.Stat(file)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    58  	if aTime != unixEpochTime {
    59  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
    60  	}
    61  
    62  	// Test both aTime and mTime set to after Unix Epoch (valid time)
    63  	Chtimes(file, afterUnixEpochTime, afterUnixEpochTime)
    64  
    65  	f, err = os.Stat(file)
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    71  	if aTime != afterUnixEpochTime {
    72  		t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime)
    73  	}
    74  
    75  	// Test both aTime and mTime set to Unix max time
    76  	Chtimes(file, unixMaxTime, unixMaxTime)
    77  
    78  	f, err = os.Stat(file)
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    84  	if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) {
    85  		t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second))
    86  	}
    87  }