aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/golang.org/x/sys/unix/syscall_linux_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/golang.org/x/sys/unix/syscall_linux_test.go')
-rw-r--r--vendor/golang.org/x/sys/unix/syscall_linux_test.go304
1 files changed, 234 insertions, 70 deletions
diff --git a/vendor/golang.org/x/sys/unix/syscall_linux_test.go b/vendor/golang.org/x/sys/unix/syscall_linux_test.go
index 2b66ad3..eed1726 100644
--- a/vendor/golang.org/x/sys/unix/syscall_linux_test.go
+++ b/vendor/golang.org/x/sys/unix/syscall_linux_test.go
@@ -7,40 +7,15 @@
package unix_test
import (
- "io/ioutil"
"os"
+ "runtime"
+ "runtime/debug"
"testing"
"time"
"golang.org/x/sys/unix"
)
-func TestFchmodat(t *testing.T) {
- defer chtmpdir(t)()
-
- touch(t, "file1")
- os.Symlink("file1", "symlink1")
-
- err := unix.Fchmodat(unix.AT_FDCWD, "symlink1", 0444, 0)
- if err != nil {
- t.Fatalf("Fchmodat: unexpected error: %v", err)
- }
-
- fi, err := os.Stat("file1")
- if err != nil {
- t.Fatal(err)
- }
-
- if fi.Mode() != 0444 {
- t.Errorf("Fchmodat: failed to change mode: expected %v, got %v", 0444, fi.Mode())
- }
-
- err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", 0444, unix.AT_SYMLINK_NOFOLLOW)
- if err != unix.EOPNOTSUPP {
- t.Fatalf("Fchmodat: unexpected error: %v, expected EOPNOTSUPP", err)
- }
-}
-
func TestIoctlGetInt(t *testing.T) {
f, err := os.Open("/dev/random")
if err != nil {
@@ -57,6 +32,10 @@ func TestIoctlGetInt(t *testing.T) {
}
func TestPpoll(t *testing.T) {
+ if runtime.GOOS == "android" {
+ t.Skip("mkfifo syscall is not available on android, skipping test")
+ }
+
f, cleanup := mktmpfifo(t)
defer cleanup()
@@ -161,20 +140,59 @@ func TestUtimesNanoAt(t *testing.T) {
if err != nil {
t.Fatalf("Lstat: %v", err)
}
- if st.Atim != ts[0] {
- t.Errorf("UtimesNanoAt: wrong atime: %v", st.Atim)
+
+ // Only check Mtim, Atim might not be supported by the underlying filesystem
+ expected := ts[1]
+ if st.Mtim.Nsec == 0 {
+ // Some filesystems only support 1-second time stamp resolution
+ // and will always set Nsec to 0.
+ expected.Nsec = 0
}
- if st.Mtim != ts[1] {
- t.Errorf("UtimesNanoAt: wrong mtime: %v", st.Mtim)
+ if st.Mtim != expected {
+ t.Errorf("UtimesNanoAt: wrong mtime: expected %v, got %v", expected, st.Mtim)
}
}
-func TestGetrlimit(t *testing.T) {
+func TestRlimitAs(t *testing.T) {
+ // disable GC during to avoid flaky test
+ defer debug.SetGCPercent(debug.SetGCPercent(-1))
+
var rlim unix.Rlimit
err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
if err != nil {
t.Fatalf("Getrlimit: %v", err)
}
+ var zero unix.Rlimit
+ if zero == rlim {
+ t.Fatalf("Getrlimit: got zero value %#v", rlim)
+ }
+ set := rlim
+ set.Cur = uint64(unix.Getpagesize())
+ err = unix.Setrlimit(unix.RLIMIT_AS, &set)
+ if err != nil {
+ t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
+ }
+
+ // RLIMIT_AS was set to the page size, so mmap()'ing twice the page size
+ // should fail. See 'man 2 getrlimit'.
+ _, err = unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
+ if err == nil {
+ t.Fatal("Mmap: unexpectedly suceeded after setting RLIMIT_AS")
+ }
+
+ err = unix.Setrlimit(unix.RLIMIT_AS, &rlim)
+ if err != nil {
+ t.Fatalf("Setrlimit: restore failed: %#v %v", rlim, err)
+ }
+
+ b, err := unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
+ if err != nil {
+ t.Fatalf("Mmap: %v", err)
+ }
+ err = unix.Munmap(b)
+ if err != nil {
+ t.Fatalf("Munmap: %v", err)
+ }
}
func TestSelect(t *testing.T) {
@@ -182,76 +200,222 @@ func TestSelect(t *testing.T) {
if err != nil {
t.Fatalf("Select: %v", err)
}
-}
-func TestFstatat(t *testing.T) {
- defer chtmpdir(t)()
+ dur := 150 * time.Millisecond
+ tv := unix.NsecToTimeval(int64(dur))
+ start := time.Now()
+ _, err = unix.Select(0, nil, nil, nil, &tv)
+ took := time.Since(start)
+ if err != nil {
+ t.Fatalf("Select: %v", err)
+ }
- touch(t, "file1")
+ if took < dur {
+ t.Errorf("Select: timeout should have been at least %v, got %v", dur, took)
+ }
+}
- var st1 unix.Stat_t
- err := unix.Stat("file1", &st1)
+func TestPselect(t *testing.T) {
+ _, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
if err != nil {
- t.Fatalf("Stat: %v", err)
+ t.Fatalf("Pselect: %v", err)
}
- var st2 unix.Stat_t
- err = unix.Fstatat(unix.AT_FDCWD, "file1", &st2, 0)
+ dur := 2500 * time.Microsecond
+ ts := unix.NsecToTimespec(int64(dur))
+ start := time.Now()
+ _, err = unix.Pselect(0, nil, nil, nil, &ts, nil)
+ took := time.Since(start)
if err != nil {
- t.Fatalf("Fstatat: %v", err)
+ t.Fatalf("Pselect: %v", err)
}
- if st1 != st2 {
- t.Errorf("Fstatat: returned stat does not match Stat")
+ if took < dur {
+ t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
}
+}
- os.Symlink("file1", "symlink1")
+func TestSchedSetaffinity(t *testing.T) {
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
- err = unix.Lstat("symlink1", &st1)
+ var oldMask unix.CPUSet
+ err := unix.SchedGetaffinity(0, &oldMask)
if err != nil {
- t.Fatalf("Lstat: %v", err)
+ t.Fatalf("SchedGetaffinity: %v", err)
+ }
+
+ var newMask unix.CPUSet
+ newMask.Zero()
+ if newMask.Count() != 0 {
+ t.Errorf("CpuZero: didn't zero CPU set: %v", newMask)
+ }
+ cpu := 1
+ newMask.Set(cpu)
+ if newMask.Count() != 1 || !newMask.IsSet(cpu) {
+ t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
+ }
+ cpu = 5
+ newMask.Set(cpu)
+ if newMask.Count() != 2 || !newMask.IsSet(cpu) {
+ t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
+ }
+ newMask.Clear(cpu)
+ if newMask.Count() != 1 || newMask.IsSet(cpu) {
+ t.Errorf("CpuClr: didn't clear CPU %d in set: %v", cpu, newMask)
+ }
+
+ if runtime.NumCPU() < 2 {
+ t.Skip("skipping setaffinity tests on single CPU system")
+ }
+ if runtime.GOOS == "android" {
+ t.Skip("skipping setaffinity tests on android")
+ }
+
+ err = unix.SchedSetaffinity(0, &newMask)
+ if err != nil {
+ t.Fatalf("SchedSetaffinity: %v", err)
}
- err = unix.Fstatat(unix.AT_FDCWD, "symlink1", &st2, unix.AT_SYMLINK_NOFOLLOW)
+ var gotMask unix.CPUSet
+ err = unix.SchedGetaffinity(0, &gotMask)
if err != nil {
- t.Fatalf("Fstatat: %v", err)
+ t.Fatalf("SchedGetaffinity: %v", err)
}
- if st1 != st2 {
- t.Errorf("Fstatat: returned stat does not match Lstat")
+ if gotMask != newMask {
+ t.Errorf("SchedSetaffinity: returned affinity mask does not match set affinity mask")
+ }
+
+ // Restore old mask so it doesn't affect successive tests
+ err = unix.SchedSetaffinity(0, &oldMask)
+ if err != nil {
+ t.Fatalf("SchedSetaffinity: %v", err)
}
}
-// utilities taken from os/os_test.go
+func TestStatx(t *testing.T) {
+ var stx unix.Statx_t
+ err := unix.Statx(unix.AT_FDCWD, ".", 0, 0, &stx)
+ if err == unix.ENOSYS || err == unix.EPERM {
+ t.Skip("statx syscall is not available, skipping test")
+ } else if err != nil {
+ t.Fatalf("Statx: %v", err)
+ }
+
+ defer chtmpdir(t)()
+ touch(t, "file1")
+
+ var st unix.Stat_t
+ err = unix.Stat("file1", &st)
+ if err != nil {
+ t.Fatalf("Stat: %v", err)
+ }
-func touch(t *testing.T, name string) {
- f, err := os.Create(name)
+ flags := unix.AT_STATX_SYNC_AS_STAT
+ err = unix.Statx(unix.AT_FDCWD, "file1", flags, unix.STATX_ALL, &stx)
if err != nil {
- t.Fatal(err)
+ t.Fatalf("Statx: %v", err)
+ }
+
+ if uint32(stx.Mode) != st.Mode {
+ t.Errorf("Statx: returned stat mode does not match Stat")
+ }
+
+ ctime := unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
+ mtime := unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
+
+ if stx.Ctime != ctime {
+ t.Errorf("Statx: returned stat ctime does not match Stat")
}
- if err := f.Close(); err != nil {
+ if stx.Mtime != mtime {
+ t.Errorf("Statx: returned stat mtime does not match Stat")
+ }
+
+ err = os.Symlink("file1", "symlink1")
+ if err != nil {
t.Fatal(err)
}
-}
-// chtmpdir changes the working directory to a new temporary directory and
-// provides a cleanup function. Used when PWD is read-only.
-func chtmpdir(t *testing.T) func() {
- oldwd, err := os.Getwd()
+ err = unix.Lstat("symlink1", &st)
if err != nil {
- t.Fatalf("chtmpdir: %v", err)
+ t.Fatalf("Lstat: %v", err)
+ }
+
+ err = unix.Statx(unix.AT_FDCWD, "symlink1", flags, unix.STATX_BASIC_STATS, &stx)
+ if err != nil {
+ t.Fatalf("Statx: %v", err)
+ }
+
+ // follow symlink, expect a regulat file
+ if stx.Mode&unix.S_IFREG == 0 {
+ t.Errorf("Statx: didn't follow symlink")
}
- d, err := ioutil.TempDir("", "test")
+
+ err = unix.Statx(unix.AT_FDCWD, "symlink1", flags|unix.AT_SYMLINK_NOFOLLOW, unix.STATX_ALL, &stx)
if err != nil {
- t.Fatalf("chtmpdir: %v", err)
+ t.Fatalf("Statx: %v", err)
+ }
+
+ // follow symlink, expect a symlink
+ if stx.Mode&unix.S_IFLNK == 0 {
+ t.Errorf("Statx: unexpectedly followed symlink")
+ }
+ if uint32(stx.Mode) != st.Mode {
+ t.Errorf("Statx: returned stat mode does not match Lstat")
+ }
+
+ ctime = unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
+ mtime = unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
+
+ if stx.Ctime != ctime {
+ t.Errorf("Statx: returned stat ctime does not match Lstat")
}
- if err := os.Chdir(d); err != nil {
- t.Fatalf("chtmpdir: %v", err)
+ if stx.Mtime != mtime {
+ t.Errorf("Statx: returned stat mtime does not match Lstat")
}
- return func() {
- if err := os.Chdir(oldwd); err != nil {
- t.Fatalf("chtmpdir: %v", err)
+}
+
+// stringsFromByteSlice converts a sequence of attributes to a []string.
+// On Linux, each entry is a NULL-terminated string.
+func stringsFromByteSlice(buf []byte) []string {
+ var result []string
+ off := 0
+ for i, b := range buf {
+ if b == 0 {
+ result = append(result, string(buf[off:i]))
+ off = i + 1
}
- os.RemoveAll(d)
+ }
+ return result
+}
+
+func TestFaccessat(t *testing.T) {
+ defer chtmpdir(t)()
+ touch(t, "file1")
+
+ err := unix.Faccessat(unix.AT_FDCWD, "file1", unix.O_RDONLY, 0)
+ if err != nil {
+ t.Errorf("Faccessat: unexpected error: %v", err)
+ }
+
+ err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.O_RDONLY, 2)
+ if err != unix.EINVAL {
+ t.Errorf("Faccessat: unexpected error: %v, want EINVAL", err)
+ }
+
+ err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.O_RDONLY, unix.AT_EACCESS)
+ if err != unix.EOPNOTSUPP {
+ t.Errorf("Faccessat: unexpected error: %v, want EOPNOTSUPP", err)
+ }
+
+ err = os.Symlink("file1", "symlink1")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = unix.Faccessat(unix.AT_FDCWD, "symlink1", unix.O_RDONLY, unix.AT_SYMLINK_NOFOLLOW)
+ if err != unix.EOPNOTSUPP {
+ t.Errorf("Faccessat: unexpected error: %v, want EOPNOTSUPP", err)
}
}