From fc0de96763280d24cfb30646c82efddb18cbb59d Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Tue, 9 Aug 2016 22:10:25 +0200 Subject: [PATCH] tests: add TestUtimesNano Make sure setting nanoseconds works by path and fd. --- tests/matrix/matrix_test.go | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tests/matrix/matrix_test.go b/tests/matrix/matrix_test.go index 1580328..853fb0e 100644 --- a/tests/matrix/matrix_test.go +++ b/tests/matrix/matrix_test.go @@ -20,6 +20,7 @@ import ( "sync" "syscall" "testing" + "time" "github.com/rfjakob/gocryptfs/internal/syscallcompat" "github.com/rfjakob/gocryptfs/tests/test_helpers" @@ -569,3 +570,70 @@ func TestLchown(t *testing.T) { t.Error(err) } } + +// Set nanoseconds by path +func TestUtimesNano(t *testing.T) { + path := test_helpers.DefaultPlainDir + "/utimesnano" + err := ioutil.WriteFile(path, []byte("foobar"), 0600) + if err != nil { + t.Fatal(err) + } + ts := make([]syscall.Timespec, 2) + // atime + ts[0].Sec = 1 + ts[0].Nsec = 2 + // mtime + ts[1].Sec = 3 + ts[1].Nsec = 4 + err = syscall.UtimesNano(path, ts) + if err != nil { + t.Fatal(err) + } + time.Sleep(1 * time.Second) + var st syscall.Stat_t + err = syscall.Stat(path, &st) + if err != nil { + t.Fatal(err) + } + if st.Atim != ts[0] { + t.Errorf("Wrong atime: %v, want: %v", st.Atim, ts[0]) + } + if st.Mtim != ts[1] { + t.Errorf("Wrong mtime: %v, want: %v", st.Mtim, ts[1]) + } +} + +// Set nanoseconds by fd +func TestUtimesNanoFd(t *testing.T) { + path := test_helpers.DefaultPlainDir + "/utimesnanofd" + f, err := os.Create(path) + if err != nil { + t.Fatal(err) + } + + ts := make([]syscall.Timespec, 2) + // atime + ts[0].Sec = 5 + ts[0].Nsec = 6 + // mtime + ts[1].Sec = 7 + ts[1].Nsec = 8 + + procPath := fmt.Sprintf("/proc/self/fd/%d", f.Fd()) + err = syscall.UtimesNano(procPath, ts) + if err != nil { + t.Fatalf("%s: %v", procPath, err) + } + + var st syscall.Stat_t + err = syscall.Stat(path, &st) + if err != nil { + t.Fatal(err) + } + if st.Atim != ts[0] { + t.Errorf("Wrong atime: %v, want: %v", st.Atim, ts[0]) + } + if st.Mtim != ts[1] { + t.Errorf("Wrong mtime: %v, want: %v", st.Mtim, ts[1]) + } +}