github.com/damirazo/docker@v1.9.0/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  // TestChtimes 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  	// 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  	aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    33  	if aTime != unixEpochTime {
    34  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
    35  	}
    36  
    37  	// Test aTime before Unix Epoch and mTime set to Unix Epoch
    38  	Chtimes(file, beforeUnixEpochTime, unixEpochTime)
    39  
    40  	f, err = os.Stat(file)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    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  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    59  	if aTime != unixEpochTime {
    60  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
    61  	}
    62  
    63  	// Test both aTime and mTime set to after Unix Epoch (valid time)
    64  	Chtimes(file, afterUnixEpochTime, afterUnixEpochTime)
    65  
    66  	f, err = os.Stat(file)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  
    71  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    72  	if aTime != afterUnixEpochTime {
    73  		t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime)
    74  	}
    75  
    76  	// Test both aTime and mTime set to Unix max time
    77  	Chtimes(file, unixMaxTime, unixMaxTime)
    78  
    79  	f, err = os.Stat(file)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    85  	if aTime != unixMaxTime {
    86  		t.Fatalf("Expected: %s, got: %s", unixMaxTime, aTime)
    87  	}
    88  
    89  	// Test aTime after Unix max time and mTime set to Unix max time
    90  	Chtimes(file, afterUnixMaxTime, unixMaxTime)
    91  
    92  	f, err = os.Stat(file)
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
    98  	if aTime != unixEpochTime {
    99  		t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime)
   100  	}
   101  
   102  	// Test aTime set to Unix Epoch and mTime before Unix Epoch
   103  	Chtimes(file, unixMaxTime, afterUnixMaxTime)
   104  
   105  	f, err = os.Stat(file)
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  	aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
   111  	if aTime != unixMaxTime {
   112  		t.Fatalf("Expected: %s, got: %s", unixMaxTime, aTime)
   113  	}
   114  }