summaryrefslogtreecommitdiffstats
path: root/src/net/file_unix_test.go
blob: 0499a02404a3ac7cd2fd1521dd04e29d6a4cc068 (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
// Copyright 2023 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"
	"testing"
)

// For backward compatibility, opening a net.Conn, turning it into an os.File,
// and calling the Fd method should return a blocking descriptor.
func TestFileFdBlocks(t *testing.T) {
	if !testableNetwork("unix") {
		t.Skipf("skipping: unix sockets not supported")
	}

	ls := newLocalServer(t, "unix")
	defer ls.teardown()

	errc := make(chan error, 1)
	done := make(chan bool)
	handler := func(ls *localServer, ln Listener) {
		server, err := ln.Accept()
		errc <- err
		if err != nil {
			return
		}
		defer server.Close()
		<-done
	}
	if err := ls.buildup(handler); err != nil {
		t.Fatal(err)
	}
	defer close(done)

	client, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
	if err != nil {
		t.Fatal(err)
	}
	defer client.Close()

	if err := <-errc; err != nil {
		t.Fatalf("server error: %v", err)
	}

	// The socket should be non-blocking.
	rawconn, err := client.(*UnixConn).SyscallConn()
	if err != nil {
		t.Fatal(err)
	}
	err = rawconn.Control(func(fd uintptr) {
		nonblock, err := unix.IsNonblock(int(fd))
		if err != nil {
			t.Fatal(err)
		}
		if !nonblock {
			t.Fatal("unix socket is in blocking mode")
		}
	})
	if err != nil {
		t.Fatal(err)
	}

	file, err := client.(*UnixConn).File()
	if err != nil {
		t.Fatal(err)
	}

	// At this point the descriptor should still be non-blocking.
	rawconn, err = file.SyscallConn()
	if err != nil {
		t.Fatal(err)
	}
	err = rawconn.Control(func(fd uintptr) {
		nonblock, err := unix.IsNonblock(int(fd))
		if err != nil {
			t.Fatal(err)
		}
		if !nonblock {
			t.Fatal("unix socket as os.File is in blocking mode")
		}
	})
	if err != nil {
		t.Fatal(err)
	}

	fd := file.Fd()

	// Calling Fd should have put the descriptor into blocking mode.
	nonblock, err := unix.IsNonblock(int(fd))
	if err != nil {
		t.Fatal(err)
	}
	if nonblock {
		t.Error("unix socket through os.File.Fd is non-blocking")
	}
}