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.
17 "golang.org/x/sys/unix"
20 func TestIoctlGetInt(t *testing.T) {
21 f, err := os.Open("/dev/random")
23 t.Fatalf("failed to open device: %v", err)
27 v, err := unix.IoctlGetInt(int(f.Fd()), unix.RNDGETENTCNT)
29 t.Fatalf("failed to perform ioctl: %v", err)
32 t.Logf("%d bits of entropy available", v)
35 func TestPpoll(t *testing.T) {
36 if runtime.GOOS == "android" {
37 t.Skip("mkfifo syscall is not available on android, skipping test")
40 f, cleanup := mktmpfifo(t)
43 const timeout = 100 * time.Millisecond
45 ok := make(chan bool, 1)
48 case <-time.After(10 * timeout):
49 t.Errorf("Ppoll: failed to timeout after %d", 10*timeout)
54 fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
55 timeoutTs := unix.NsecToTimespec(int64(timeout))
56 n, err := unix.Ppoll(fds, &timeoutTs, nil)
59 t.Errorf("Ppoll: unexpected error: %v", err)
63 t.Errorf("Ppoll: wrong number of events: got %v, expected %v", n, 0)
68 func TestTime(t *testing.T) {
70 ut2, err := unix.Time(&ut)
72 t.Fatalf("Time: %v", err)
75 t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
80 for i := 0; i < 10; i++ {
81 ut, err = unix.Time(nil)
83 t.Fatalf("Time: %v", err)
88 if int64(ut) == now.Unix() {
93 t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
96 func TestUtime(t *testing.T) {
101 buf := &unix.Utimbuf{
105 err := unix.Utime("file1", buf)
107 t.Fatalf("Utime: %v", err)
110 fi, err := os.Stat("file1")
115 if fi.ModTime().Unix() != 12345 {
116 t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
120 func TestUtimesNanoAt(t *testing.T) {
123 symlink := "symlink1"
125 err := os.Symlink("nonexisting", symlink)
130 ts := []unix.Timespec{
131 {Sec: 1111, Nsec: 2222},
132 {Sec: 3333, Nsec: 4444},
134 err = unix.UtimesNanoAt(unix.AT_FDCWD, symlink, ts, unix.AT_SYMLINK_NOFOLLOW)
136 t.Fatalf("UtimesNanoAt: %v", err)
140 err = unix.Lstat(symlink, &st)
142 t.Fatalf("Lstat: %v", err)
145 // Only check Mtim, Atim might not be supported by the underlying filesystem
147 if st.Mtim.Nsec == 0 {
148 // Some filesystems only support 1-second time stamp resolution
149 // and will always set Nsec to 0.
152 if st.Mtim != expected {
153 t.Errorf("UtimesNanoAt: wrong mtime: expected %v, got %v", expected, st.Mtim)
157 func TestRlimitAs(t *testing.T) {
158 // disable GC during to avoid flaky test
159 defer debug.SetGCPercent(debug.SetGCPercent(-1))
162 err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
164 t.Fatalf("Getrlimit: %v", err)
168 t.Fatalf("Getrlimit: got zero value %#v", rlim)
171 set.Cur = uint64(unix.Getpagesize())
172 err = unix.Setrlimit(unix.RLIMIT_AS, &set)
174 t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
177 // RLIMIT_AS was set to the page size, so mmap()'ing twice the page size
178 // should fail. See 'man 2 getrlimit'.
179 _, err = unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
181 t.Fatal("Mmap: unexpectedly succeeded after setting RLIMIT_AS")
184 err = unix.Setrlimit(unix.RLIMIT_AS, &rlim)
186 t.Fatalf("Setrlimit: restore failed: %#v %v", rlim, err)
189 b, err := unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
191 t.Fatalf("Mmap: %v", err)
195 t.Fatalf("Munmap: %v", err)
199 func TestSelect(t *testing.T) {
200 _, err := unix.Select(0, nil, nil, nil, &unix.Timeval{Sec: 0, Usec: 0})
202 t.Fatalf("Select: %v", err)
205 dur := 150 * time.Millisecond
206 tv := unix.NsecToTimeval(int64(dur))
208 _, err = unix.Select(0, nil, nil, nil, &tv)
209 took := time.Since(start)
211 t.Fatalf("Select: %v", err)
215 t.Errorf("Select: timeout should have been at least %v, got %v", dur, took)
219 func TestPselect(t *testing.T) {
220 _, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
222 t.Fatalf("Pselect: %v", err)
225 dur := 2500 * time.Microsecond
226 ts := unix.NsecToTimespec(int64(dur))
228 _, err = unix.Pselect(0, nil, nil, nil, &ts, nil)
229 took := time.Since(start)
231 t.Fatalf("Pselect: %v", err)
235 t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
239 func TestSchedSetaffinity(t *testing.T) {
240 runtime.LockOSThread()
241 defer runtime.UnlockOSThread()
243 var oldMask unix.CPUSet
244 err := unix.SchedGetaffinity(0, &oldMask)
246 t.Fatalf("SchedGetaffinity: %v", err)
249 var newMask unix.CPUSet
251 if newMask.Count() != 0 {
252 t.Errorf("CpuZero: didn't zero CPU set: %v", newMask)
256 if newMask.Count() != 1 || !newMask.IsSet(cpu) {
257 t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
261 if newMask.Count() != 2 || !newMask.IsSet(cpu) {
262 t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
265 if newMask.Count() != 1 || newMask.IsSet(cpu) {
266 t.Errorf("CpuClr: didn't clear CPU %d in set: %v", cpu, newMask)
269 if runtime.NumCPU() < 2 {
270 t.Skip("skipping setaffinity tests on single CPU system")
272 if runtime.GOOS == "android" {
273 t.Skip("skipping setaffinity tests on android")
276 // On a system like ppc64x where some cores can be disabled using ppc64_cpu,
277 // setaffinity should only be called with enabled cores. The valid cores
278 // are found from the oldMask, but if none are found then the setaffinity
279 // tests are skipped. Issue #27875.
280 if !oldMask.IsSet(cpu) {
282 for i := 0; i < len(oldMask); i++ {
283 if oldMask.IsSet(i) {
288 if newMask.Count() == 0 {
289 t.Skip("skipping setaffinity tests if CPU not available")
293 err = unix.SchedSetaffinity(0, &newMask)
295 t.Fatalf("SchedSetaffinity: %v", err)
298 var gotMask unix.CPUSet
299 err = unix.SchedGetaffinity(0, &gotMask)
301 t.Fatalf("SchedGetaffinity: %v", err)
304 if gotMask != newMask {
305 t.Errorf("SchedSetaffinity: returned affinity mask does not match set affinity mask")
308 // Restore old mask so it doesn't affect successive tests
309 err = unix.SchedSetaffinity(0, &oldMask)
311 t.Fatalf("SchedSetaffinity: %v", err)
315 func TestStatx(t *testing.T) {
317 err := unix.Statx(unix.AT_FDCWD, ".", 0, 0, &stx)
318 if err == unix.ENOSYS || err == unix.EPERM {
319 t.Skip("statx syscall is not available, skipping test")
320 } else if err != nil {
321 t.Fatalf("Statx: %v", err)
328 err = unix.Stat("file1", &st)
330 t.Fatalf("Stat: %v", err)
333 flags := unix.AT_STATX_SYNC_AS_STAT
334 err = unix.Statx(unix.AT_FDCWD, "file1", flags, unix.STATX_ALL, &stx)
336 t.Fatalf("Statx: %v", err)
339 if uint32(stx.Mode) != st.Mode {
340 t.Errorf("Statx: returned stat mode does not match Stat")
343 ctime := unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
344 mtime := unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
346 if stx.Ctime != ctime {
347 t.Errorf("Statx: returned stat ctime does not match Stat")
349 if stx.Mtime != mtime {
350 t.Errorf("Statx: returned stat mtime does not match Stat")
353 err = os.Symlink("file1", "symlink1")
358 err = unix.Lstat("symlink1", &st)
360 t.Fatalf("Lstat: %v", err)
363 err = unix.Statx(unix.AT_FDCWD, "symlink1", flags, unix.STATX_BASIC_STATS, &stx)
365 t.Fatalf("Statx: %v", err)
368 // follow symlink, expect a regulat file
369 if stx.Mode&unix.S_IFREG == 0 {
370 t.Errorf("Statx: didn't follow symlink")
373 err = unix.Statx(unix.AT_FDCWD, "symlink1", flags|unix.AT_SYMLINK_NOFOLLOW, unix.STATX_ALL, &stx)
375 t.Fatalf("Statx: %v", err)
378 // follow symlink, expect a symlink
379 if stx.Mode&unix.S_IFLNK == 0 {
380 t.Errorf("Statx: unexpectedly followed symlink")
382 if uint32(stx.Mode) != st.Mode {
383 t.Errorf("Statx: returned stat mode does not match Lstat")
386 ctime = unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
387 mtime = unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
389 if stx.Ctime != ctime {
390 t.Errorf("Statx: returned stat ctime does not match Lstat")
392 if stx.Mtime != mtime {
393 t.Errorf("Statx: returned stat mtime does not match Lstat")
397 // stringsFromByteSlice converts a sequence of attributes to a []string.
398 // On Linux, each entry is a NULL-terminated string.
399 func stringsFromByteSlice(buf []byte) []string {
402 for i, b := range buf {
404 result = append(result, string(buf[off:i]))
411 func TestFaccessat(t *testing.T) {
415 err := unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, 0)
417 t.Errorf("Faccessat: unexpected error: %v", err)
420 err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, 2)
421 if err != unix.EINVAL {
422 t.Errorf("Faccessat: unexpected error: %v, want EINVAL", err)
425 err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, unix.AT_EACCESS)
427 t.Errorf("Faccessat: unexpected error: %v", err)
430 err = os.Symlink("file1", "symlink1")
435 err = unix.Faccessat(unix.AT_FDCWD, "symlink1", unix.R_OK, unix.AT_SYMLINK_NOFOLLOW)
437 t.Errorf("Faccessat SYMLINK_NOFOLLOW: unexpected error %v", err)
440 // We can't really test AT_SYMLINK_NOFOLLOW, because there
441 // doesn't seem to be any way to change the mode of a symlink.
442 // We don't test AT_EACCESS because such tests are only
443 // meaningful if run as root.
445 err = unix.Fchmodat(unix.AT_FDCWD, "file1", 0, 0)
447 t.Errorf("Fchmodat: unexpected error %v", err)
450 err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.F_OK, unix.AT_SYMLINK_NOFOLLOW)
452 t.Errorf("Faccessat: unexpected error: %v", err)
455 err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, unix.AT_SYMLINK_NOFOLLOW)
456 if err != unix.EACCES {
457 if unix.Getuid() != 0 {
458 t.Errorf("Faccessat: unexpected error: %v, want EACCES", err)
463 func TestSyncFileRange(t *testing.T) {
464 file, err := ioutil.TempFile("", "TestSyncFileRange")
468 defer os.Remove(file.Name())
471 err = unix.SyncFileRange(int(file.Fd()), 0, 0, 0)
472 if err == unix.ENOSYS || err == unix.EPERM {
473 t.Skip("sync_file_range syscall is not available, skipping test")
474 } else if err != nil {
475 t.Fatalf("SyncFileRange: %v", err)
480 err = unix.SyncFileRange(int(file.Fd()), 0, 0, flags)
481 if err != unix.EINVAL {
482 t.Fatalf("SyncFileRange: unexpected error: %v, want EINVAL", err)