1 // Copyright 2021 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.
10 "internal/syscall/unix"
17 func TestUnixConnReadMsgUnixSCMRightsCloseOnExec(t *testing.T) {
18 if !testableNetwork("unix") {
19 t.Skip("not unix system")
22 scmFile, err := os.Open(os.DevNull)
24 t.Fatalf("file open: %v", err)
28 rights := syscall.UnixRights(int(scmFile.Fd()))
29 fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
31 t.Fatalf("Socketpair: %v", err)
34 writeFile := os.NewFile(uintptr(fds[0]), "write-socket")
35 defer writeFile.Close()
36 readFile := os.NewFile(uintptr(fds[1]), "read-socket")
37 defer readFile.Close()
39 cw, err := FileConn(writeFile)
41 t.Fatalf("FileConn: %v", err)
44 cr, err := FileConn(readFile)
46 t.Fatalf("FileConn: %v", err)
50 ucw, ok := cw.(*UnixConn)
52 t.Fatalf("got %T; want UnixConn", cw)
54 ucr, ok := cr.(*UnixConn)
56 t.Fatalf("got %T; want UnixConn", cr)
59 oob := make([]byte, syscall.CmsgSpace(4))
60 err = ucw.SetWriteDeadline(time.Now().Add(5 * time.Second))
62 t.Fatalf("Can't set unix connection timeout: %v", err)
64 _, _, err = ucw.WriteMsgUnix(nil, rights, nil)
66 t.Fatalf("UnixConn readMsg: %v", err)
68 err = ucr.SetReadDeadline(time.Now().Add(5 * time.Second))
70 t.Fatalf("Can't set unix connection timeout: %v", err)
72 _, oobn, _, _, err := ucr.ReadMsgUnix(nil, oob)
74 t.Fatalf("UnixConn readMsg: %v", err)
77 scms, err := syscall.ParseSocketControlMessage(oob[:oobn])
79 t.Fatalf("ParseSocketControlMessage: %v", err)
82 t.Fatalf("got scms = %#v; expected 1 SocketControlMessage", scms)
85 gotFDs, err := syscall.ParseUnixRights(&scm)
87 t.Fatalf("syscall.ParseUnixRights: %v", err)
90 t.Fatalf("got FDs %#v: wanted only 1 fd", gotFDs)
93 if err := syscall.Close(gotFDs[0]); err != nil {
94 t.Fatalf("fail to close gotFDs: %v", err)
98 flags, err := unix.Fcntl(gotFDs[0], syscall.F_GETFD, 0)
100 t.Fatalf("Can't get flags of fd:%#v, with err:%v", gotFDs[0], err)
102 if flags&syscall.FD_CLOEXEC == 0 {
103 t.Fatalf("got flags %#x, want %#x (FD_CLOEXEC) set", flags, syscall.FD_CLOEXEC)