1 // Copyright 2013 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.
5 // +build darwin dragonfly freebsd linux netbsd openbsd solaris
21 "golang.org/x/sys/unix"
24 // Tests that below functions, structures and constants are consistent
25 // on all Unix-like systems.
27 // program scheduling priority functions and constants
29 _ func(int, int, int) error = unix.Setpriority
30 _ func(int, int) (int, error) = unix.Getpriority
33 _ int = unix.PRIO_USER
34 _ int = unix.PRIO_PROCESS
35 _ int = unix.PRIO_PGRP
41 _ int = unix.TCIOFLUSH
45 // fcntl file locking structure and constants
62 // TestFcntlFlock tests whether the file locking structure matches
63 // the calling convention of each kernel.
64 func TestFcntlFlock(t *testing.T) {
65 name := filepath.Join(os.TempDir(), "TestFcntlFlock")
66 fd, err := unix.Open(name, unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC, 0)
68 t.Fatalf("Open failed: %v", err)
70 defer unix.Unlink(name)
72 flock := unix.Flock_t{
74 Start: 0, Len: 0, Whence: 1,
76 if err := unix.FcntlFlock(uintptr(fd), unix.F_GETLK, &flock); err != nil {
77 t.Fatalf("FcntlFlock failed: %v", err)
81 // TestPassFD tests passing a file descriptor over a Unix socket.
83 // This test involved both a parent and child process. The parent
84 // process is invoked as a normal test, with "go test", which then
85 // runs the child process by running the current test binary with args
86 // "-test.run=^TestPassFD$" and an environment variable used to signal
87 // that the test should become the child process instead.
88 func TestPassFD(t *testing.T) {
91 // TODO(jsing): Figure out why sendmsg is returning EINVAL.
92 t.Skip("skipping test on dragonfly")
94 // TODO(aram): Figure out why ReadMsgUnix is returning empty message.
95 t.Skip("skipping test on solaris, see issue 7402")
97 if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
102 tempDir, err := ioutil.TempDir("", "TestPassFD")
106 defer os.RemoveAll(tempDir)
108 fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_STREAM, 0)
110 t.Fatalf("Socketpair: %v", err)
112 defer unix.Close(fds[0])
113 defer unix.Close(fds[1])
114 writeFile := os.NewFile(uintptr(fds[0]), "child-writes")
115 readFile := os.NewFile(uintptr(fds[1]), "parent-reads")
116 defer writeFile.Close()
117 defer readFile.Close()
119 cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", tempDir)
120 cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
121 if lp := os.Getenv("LD_LIBRARY_PATH"); lp != "" {
122 cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH="+lp)
124 cmd.ExtraFiles = []*os.File{writeFile}
126 out, err := cmd.CombinedOutput()
127 if len(out) > 0 || err != nil {
128 t.Fatalf("child process: %q, %v", out, err)
131 c, err := net.FileConn(readFile)
133 t.Fatalf("FileConn: %v", err)
137 uc, ok := c.(*net.UnixConn)
139 t.Fatalf("unexpected FileConn type; expected UnixConn, got %T", c)
142 buf := make([]byte, 32) // expect 1 byte
143 oob := make([]byte, 32) // expect 24 bytes
144 closeUnix := time.AfterFunc(5*time.Second, func() {
145 t.Logf("timeout reading from unix socket")
148 _, oobn, _, _, err := uc.ReadMsgUnix(buf, oob)
151 scms, err := unix.ParseSocketControlMessage(oob[:oobn])
153 t.Fatalf("ParseSocketControlMessage: %v", err)
156 t.Fatalf("expected 1 SocketControlMessage; got scms = %#v", scms)
159 gotFds, err := unix.ParseUnixRights(&scm)
161 t.Fatalf("unix.ParseUnixRights: %v", err)
163 if len(gotFds) != 1 {
164 t.Fatalf("wanted 1 fd; got %#v", gotFds)
167 f := os.NewFile(uintptr(gotFds[0]), "fd-from-child")
170 got, err := ioutil.ReadAll(f)
171 want := "Hello from child process!\n"
172 if string(got) != want {
173 t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want)
177 // passFDChild is the child process used by TestPassFD.
181 // Look for our fd. It should be fd 3, but we work around an fd leak
182 // bug here (http://golang.org/issue/2603) to let it be elsewhere.
184 for fd := uintptr(3); fd <= 10; fd++ {
185 f := os.NewFile(fd, "unix-conn")
187 netc, _ := net.FileConn(f)
188 uc, ok = netc.(*net.UnixConn)
194 fmt.Println("failed to find unix fd")
198 // Make a file f to send to our parent process on uc.
199 // We make it in tempDir, which our parent will clean up.
201 tempDir := flag.Arg(0)
202 f, err := ioutil.TempFile(tempDir, "")
204 fmt.Printf("TempFile: %v", err)
208 f.Write([]byte("Hello from child process!\n"))
211 rights := unix.UnixRights(int(f.Fd()))
212 dummyByte := []byte("x")
213 n, oobn, err := uc.WriteMsgUnix(dummyByte, rights, nil)
215 fmt.Printf("WriteMsgUnix: %v", err)
218 if n != 1 || oobn != len(rights) {
219 fmt.Printf("WriteMsgUnix = %d, %d; want 1, %d", n, oobn, len(rights))
224 // TestUnixRightsRoundtrip tests that UnixRights, ParseSocketControlMessage,
225 // and ParseUnixRights are able to successfully round-trip lists of file descriptors.
226 func TestUnixRightsRoundtrip(t *testing.T) {
227 testCases := [...][][]int{
232 {{1, 2}, {3, 4, 5}, {}, {7}},
234 for _, testCase := range testCases {
237 for _, fds := range testCase {
238 // Last assignment to n wins
239 n = len(b) + unix.CmsgLen(4*len(fds))
240 b = append(b, unix.UnixRights(fds...)...)
245 scms, err := unix.ParseSocketControlMessage(b)
247 t.Fatalf("ParseSocketControlMessage: %v", err)
249 if len(scms) != len(testCase) {
250 t.Fatalf("expected %v SocketControlMessage; got scms = %#v", len(testCase), scms)
252 for i, scm := range scms {
253 gotFds, err := unix.ParseUnixRights(&scm)
255 t.Fatalf("ParseUnixRights: %v", err)
257 wantFds := testCase[i]
258 if len(gotFds) != len(wantFds) {
259 t.Fatalf("expected %v fds, got %#v", len(wantFds), gotFds)
261 for j, fd := range gotFds {
262 if fd != wantFds[j] {
263 t.Fatalf("expected fd %v, got %v", wantFds[j], fd)
270 func TestRlimit(t *testing.T) {
271 var rlimit, zero unix.Rlimit
272 err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlimit)
274 t.Fatalf("Getrlimit: save failed: %v", err)
277 t.Fatalf("Getrlimit: save failed: got zero value %#v", rlimit)
280 set.Cur = set.Max - 1
281 err = unix.Setrlimit(unix.RLIMIT_NOFILE, &set)
283 t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
286 err = unix.Getrlimit(unix.RLIMIT_NOFILE, &get)
288 t.Fatalf("Getrlimit: get failed: %v", err)
291 set.Cur = set.Max - 1
293 // Seems like Darwin requires some privilege to
294 // increase the soft limit of rlimit sandbox, though
295 // Setrlimit never reports an error.
296 switch runtime.GOOS {
299 t.Fatalf("Rlimit: change failed: wanted %#v got %#v", set, get)
302 err = unix.Setrlimit(unix.RLIMIT_NOFILE, &rlimit)
304 t.Fatalf("Setrlimit: restore failed: %#v %v", rlimit, err)
308 func TestSeekFailure(t *testing.T) {
309 _, err := unix.Seek(-1, 0, 0)
311 t.Fatalf("Seek(-1, 0, 0) did not fail")
313 str := err.Error() // used to crash on Linux
314 t.Logf("Seek: %v", str)
316 t.Fatalf("Seek(-1, 0, 0) return error with empty message")
320 func TestDup(t *testing.T) {
321 file, err := ioutil.TempFile("", "TestDup")
323 t.Fatalf("Tempfile failed: %v", err)
325 defer os.Remove(file.Name())
329 newFd, err := unix.Dup(f)
331 t.Fatalf("Dup: %v", err)
334 err = unix.Dup2(newFd, newFd+1)
336 t.Fatalf("Dup2: %v", err)
339 b1 := []byte("Test123")
340 b2 := make([]byte, 7)
341 _, err = unix.Write(newFd+1, b1)
343 t.Fatalf("Write to dup2 fd failed: %v", err)
345 _, err = unix.Seek(f, 0, 0)
346 _, err = unix.Read(f, b2)
348 t.Fatalf("Read back failed: %v", err)
350 if string(b1) != string(b2) {
351 t.Errorf("Dup: stdout write not in file, expected %v, got %v", string(b1), string(b2))