1 // Copyright 2016 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.
15 "golang.org/x/sys/unix"
18 func TestFchmodat(t *testing.T) {
22 os.Symlink("file1", "symlink1")
24 err := unix.Fchmodat(unix.AT_FDCWD, "symlink1", 0444, 0)
26 t.Fatalf("Fchmodat: unexpected error: %v", err)
29 fi, err := os.Stat("file1")
34 if fi.Mode() != 0444 {
35 t.Errorf("Fchmodat: failed to change mode: expected %v, got %v", 0444, fi.Mode())
38 err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", 0444, unix.AT_SYMLINK_NOFOLLOW)
39 if err != unix.EOPNOTSUPP {
40 t.Fatalf("Fchmodat: unexpected error: %v, expected EOPNOTSUPP", err)
44 func TestIoctlGetInt(t *testing.T) {
45 f, err := os.Open("/dev/random")
47 t.Fatalf("failed to open device: %v", err)
51 v, err := unix.IoctlGetInt(int(f.Fd()), unix.RNDGETENTCNT)
53 t.Fatalf("failed to perform ioctl: %v", err)
56 t.Logf("%d bits of entropy available", v)
59 func TestPoll(t *testing.T) {
60 f, cleanup := mktmpfifo(t)
65 ok := make(chan bool, 1)
68 case <-time.After(10 * timeout * time.Millisecond):
69 t.Errorf("Poll: failed to timeout after %d milliseconds", 10*timeout)
74 fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
75 n, err := unix.Poll(fds, timeout)
78 t.Errorf("Poll: unexpected error: %v", err)
82 t.Errorf("Poll: wrong number of events: got %v, expected %v", n, 0)
87 func TestPpoll(t *testing.T) {
88 f, cleanup := mktmpfifo(t)
91 const timeout = 100 * time.Millisecond
93 ok := make(chan bool, 1)
96 case <-time.After(10 * timeout):
97 t.Errorf("Ppoll: failed to timeout after %d", 10*timeout)
102 fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
103 timeoutTs := unix.NsecToTimespec(int64(timeout))
104 n, err := unix.Ppoll(fds, &timeoutTs, nil)
107 t.Errorf("Ppoll: unexpected error: %v", err)
111 t.Errorf("Ppoll: wrong number of events: got %v, expected %v", n, 0)
116 // mktmpfifo creates a temporary FIFO and provides a cleanup function.
117 func mktmpfifo(t *testing.T) (*os.File, func()) {
118 err := unix.Mkfifo("fifo", 0666)
120 t.Fatalf("mktmpfifo: failed to create FIFO: %v", err)
123 f, err := os.OpenFile("fifo", os.O_RDWR, 0666)
126 t.Fatalf("mktmpfifo: failed to open FIFO: %v", err)
135 func TestTime(t *testing.T) {
137 ut2, err := unix.Time(&ut)
139 t.Fatalf("Time: %v", err)
142 t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
147 for i := 0; i < 10; i++ {
148 ut, err = unix.Time(nil)
150 t.Fatalf("Time: %v", err)
155 if int64(ut) == now.Unix() {
160 t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
163 func TestUtime(t *testing.T) {
168 buf := &unix.Utimbuf{
172 err := unix.Utime("file1", buf)
174 t.Fatalf("Utime: %v", err)
177 fi, err := os.Stat("file1")
182 if fi.ModTime().Unix() != 12345 {
183 t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
187 func TestGetrlimit(t *testing.T) {
189 err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
191 t.Fatalf("Getrlimit: %v", err)
195 // utilities taken from os/os_test.go
197 func touch(t *testing.T, name string) {
198 f, err := os.Create(name)
202 if err := f.Close(); err != nil {
207 // chtmpdir changes the working directory to a new temporary directory and
208 // provides a cleanup function. Used when PWD is read-only.
209 func chtmpdir(t *testing.T) func() {
210 oldwd, err := os.Getwd()
212 t.Fatalf("chtmpdir: %v", err)
214 d, err := ioutil.TempDir("", "test")
216 t.Fatalf("chtmpdir: %v", err)
218 if err := os.Chdir(d); err != nil {
219 t.Fatalf("chtmpdir: %v", err)
222 if err := os.Chdir(oldwd); err != nil {
223 t.Fatalf("chtmpdir: %v", err)