summaryrefslogtreecommitdiffstats
path: root/src/net/unixsock_readmsg_test.go
blob: 2d89dc4936a12904183ac85dbaa0ff560bfb9975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build unix

package net

import (
	"internal/syscall/unix"
	"os"
	"syscall"
	"testing"
	"time"
)

func TestUnixConnReadMsgUnixSCMRightsCloseOnExec(t *testing.T) {
	if !testableNetwork("unix") {
		t.Skip("not unix system")
	}

	scmFile, err := os.Open(os.DevNull)
	if err != nil {
		t.Fatalf("file open: %v", err)
	}
	defer scmFile.Close()

	rights := syscall.UnixRights(int(scmFile.Fd()))
	fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
	if err != nil {
		t.Fatalf("Socketpair: %v", err)
	}

	writeFile := os.NewFile(uintptr(fds[0]), "write-socket")
	defer writeFile.Close()
	readFile := os.NewFile(uintptr(fds[1]), "read-socket")
	defer readFile.Close()

	cw, err := FileConn(writeFile)
	if err != nil {
		t.Fatalf("FileConn: %v", err)
	}
	defer cw.Close()
	cr, err := FileConn(readFile)
	if err != nil {
		t.Fatalf("FileConn: %v", err)
	}
	defer cr.Close()

	ucw, ok := cw.(*UnixConn)
	if !ok {
		t.Fatalf("got %T; want UnixConn", cw)
	}
	ucr, ok := cr.(*UnixConn)
	if !ok {
		t.Fatalf("got %T; want UnixConn", cr)
	}

	oob := make([]byte, syscall.CmsgSpace(4))
	err = ucw.SetWriteDeadline(time.Now().Add(5 * time.Second))
	if err != nil {
		t.Fatalf("Can't set unix connection timeout: %v", err)
	}
	_, _, err = ucw.WriteMsgUnix(nil, rights, nil)
	if err != nil {
		t.Fatalf("UnixConn readMsg: %v", err)
	}
	err = ucr.SetReadDeadline(time.Now().Add(5 * time.Second))
	if err != nil {
		t.Fatalf("Can't set unix connection timeout: %v", err)
	}
	_, oobn, _, _, err := ucr.ReadMsgUnix(nil, oob)
	if err != nil {
		t.Fatalf("UnixConn readMsg: %v", err)
	}

	scms, err := syscall.ParseSocketControlMessage(oob[:oobn])
	if err != nil {
		t.Fatalf("ParseSocketControlMessage: %v", err)
	}
	if len(scms) != 1 {
		t.Fatalf("got scms = %#v; expected 1 SocketControlMessage", scms)
	}
	scm := scms[0]
	gotFDs, err := syscall.ParseUnixRights(&scm)
	if err != nil {
		t.Fatalf("syscall.ParseUnixRights: %v", err)
	}
	if len(gotFDs) != 1 {
		t.Fatalf("got FDs %#v: wanted only 1 fd", gotFDs)
	}
	defer func() {
		if err := syscall.Close(gotFDs[0]); err != nil {
			t.Fatalf("fail to close gotFDs: %v", err)
		}
	}()

	flags, err := unix.Fcntl(gotFDs[0], syscall.F_GETFD, 0)
	if err != nil {
		t.Fatalf("Can't get flags of fd:%#v, with err:%v", gotFDs[0], err)
	}
	if flags&syscall.FD_CLOEXEC == 0 {
		t.Fatalf("got flags %#x, want %#x (FD_CLOEXEC) set", flags, syscall.FD_CLOEXEC)
	}
}