1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package unix_test
6
7import (
8	"os"
9	"testing"
10
11	"golang.org/x/sys/unix"
12)
13
14// stringsFromByteSlice converts a sequence of attributes to a []string.
15// On Darwin, each entry is a NULL-terminated string.
16func stringsFromByteSlice(buf []byte) []string {
17	var result []string
18	off := 0
19	for i, b := range buf {
20		if b == 0 {
21			result = append(result, string(buf[off:i]))
22			off = i + 1
23		}
24	}
25	return result
26}
27
28func TestUtimesNanoAt(t *testing.T) {
29	defer chtmpdir(t)()
30
31	symlink := "symlink1"
32	os.Remove(symlink)
33	err := os.Symlink("nonexisting", symlink)
34	if err != nil {
35		t.Fatal(err)
36	}
37
38	ts := []unix.Timespec{
39		{Sec: 1111, Nsec: 2222},
40		{Sec: 3333, Nsec: 4444},
41	}
42	err = unix.UtimesNanoAt(unix.AT_FDCWD, symlink, ts, unix.AT_SYMLINK_NOFOLLOW)
43	if err != nil {
44		t.Fatalf("UtimesNanoAt: %v", err)
45	}
46
47	var st unix.Stat_t
48	err = unix.Lstat(symlink, &st)
49	if err != nil {
50		t.Fatalf("Lstat: %v", err)
51	}
52
53	// Only check Mtim, Atim might not be supported by the underlying filesystem
54	expected := ts[1]
55	if st.Mtim.Nsec == 0 {
56		// Some filesystems only support 1-second time stamp resolution
57		// and will always set Nsec to 0.
58		expected.Nsec = 0
59	}
60	if st.Mtim != expected {
61		t.Errorf("UtimesNanoAt: wrong mtime: got %v, expected %v", st.Mtim, expected)
62	}
63}
64
65func TestSysctlClockinfo(t *testing.T) {
66	ci, err := unix.SysctlClockinfo("kern.clockrate")
67	if err != nil {
68		t.Fatal(err)
69	}
70	t.Logf("tick = %v, tickadj = %v, hz = %v, profhz = %v, stathz = %v",
71		ci.Tick, ci.Tickadj, ci.Hz, ci.Profhz, ci.Stathz)
72}
73