浏览代码

unix: test UtimesNanoAt on darwin

Change-Id: I18354d29db909227ae75df1c8b1ab7888375f8db
Reviewed-on: https://go-review.googlesource.com/c/147740
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Tobias Klauser 7 年之前
父节点
当前提交
3a76605856
共有 1 个文件被更改,包括 44 次插入0 次删除
  1. 44 0
      unix/syscall_darwin_test.go

+ 44 - 0
unix/syscall_darwin_test.go

@@ -4,6 +4,13 @@
 
 package unix_test
 
+import (
+	"os"
+	"testing"
+
+	"golang.org/x/sys/unix"
+)
+
 // stringsFromByteSlice converts a sequence of attributes to a []string.
 // On Darwin, each entry is a NULL-terminated string.
 func stringsFromByteSlice(buf []byte) []string {
@@ -17,3 +24,40 @@ func stringsFromByteSlice(buf []byte) []string {
 	}
 	return result
 }
+
+func TestUtimesNanoAt(t *testing.T) {
+	defer chtmpdir(t)()
+
+	symlink := "symlink1"
+	os.Remove(symlink)
+	err := os.Symlink("nonexisting", symlink)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	ts := []unix.Timespec{
+		{Sec: 1111, Nsec: 2222},
+		{Sec: 3333, Nsec: 4444},
+	}
+	err = unix.UtimesNanoAt(unix.AT_FDCWD, symlink, ts, unix.AT_SYMLINK_NOFOLLOW)
+	if err != nil {
+		t.Fatalf("UtimesNanoAt: %v", err)
+	}
+
+	var st unix.Stat_t
+	err = unix.Lstat(symlink, &st)
+	if err != nil {
+		t.Fatalf("Lstat: %v", err)
+	}
+
+	// Only check Mtimespec, Atimespec might not be supported by the underlying filesystem
+	expected := ts[1]
+	if st.Mtimespec.Nsec == 0 {
+		// Some filesystems only support 1-second time stamp resolution
+		// and will always set Nsec to 0.
+		expected.Nsec = 0
+	}
+	if st.Mtimespec != expected {
+		t.Errorf("UtimesNanoAt: wrong mtime: got %v, expected %v", st.Mtimespec, expected)
+	}
+}