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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
// Copyright 2011 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.
package net
import (
"os"
"reflect"
"runtime"
"testing"
)
type listenerFile interface {
Listener
File() (f *os.File, err error)
}
type packetConnFile interface {
PacketConn
File() (f *os.File, err error)
}
type connFile interface {
Conn
File() (f *os.File, err error)
}
func testFileListener(t *testing.T, net, laddr string) {
switch net {
case "tcp", "tcp4", "tcp6":
laddr += ":0" // any available port
}
l, err := Listen(net, laddr)
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer l.Close()
lf := l.(listenerFile)
f, err := lf.File()
if err != nil {
t.Fatalf("File failed: %v", err)
}
c, err := FileListener(f)
if err != nil {
t.Fatalf("FileListener failed: %v", err)
}
if !reflect.DeepEqual(l.Addr(), c.Addr()) {
t.Fatalf("Addrs not equal: %#v != %#v", l.Addr(), c.Addr())
}
if err := c.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
}
var fileListenerTests = []struct {
net string
laddr string
ipv6 bool // test with underlying AF_INET6 socket
linux bool // test with abstract unix domain socket, a Linux-ism
}{
{net: "tcp", laddr: ""},
{net: "tcp", laddr: "0.0.0.0"},
{net: "tcp", laddr: "[::ffff:0.0.0.0]"},
{net: "tcp", laddr: "[::]", ipv6: true},
{net: "tcp", laddr: "127.0.0.1"},
{net: "tcp", laddr: "[::ffff:127.0.0.1]"},
{net: "tcp", laddr: "[::1]", ipv6: true},
{net: "tcp4", laddr: ""},
{net: "tcp4", laddr: "0.0.0.0"},
{net: "tcp4", laddr: "[::ffff:0.0.0.0]"},
{net: "tcp4", laddr: "127.0.0.1"},
{net: "tcp4", laddr: "[::ffff:127.0.0.1]"},
{net: "tcp6", laddr: "", ipv6: true},
{net: "tcp6", laddr: "[::]", ipv6: true},
{net: "tcp6", laddr: "[::1]", ipv6: true},
{net: "unix", laddr: "@gotest/net", linux: true},
{net: "unixpacket", laddr: "@gotest/net", linux: true},
}
func TestFileListener(t *testing.T) {
switch runtime.GOOS {
case "windows":
t.Skipf("skipping test on %q", runtime.GOOS)
}
for _, tt := range fileListenerTests {
if skipServerTest(tt.net, "unix", tt.laddr, tt.ipv6, false, tt.linux) {
continue
}
if skipServerTest(tt.net, "unixpacket", tt.laddr, tt.ipv6, false, tt.linux) {
continue
}
testFileListener(t, tt.net, tt.laddr)
}
}
func testFilePacketConn(t *testing.T, pcf packetConnFile, listen bool) {
f, err := pcf.File()
if err != nil {
t.Fatalf("File failed: %v", err)
}
c, err := FilePacketConn(f)
if err != nil {
t.Fatalf("FilePacketConn failed: %v", err)
}
if !reflect.DeepEqual(pcf.LocalAddr(), c.LocalAddr()) {
t.Fatalf("LocalAddrs not equal: %#v != %#v", pcf.LocalAddr(), c.LocalAddr())
}
if listen {
if _, err := c.WriteTo([]byte{}, c.LocalAddr()); err != nil {
t.Fatalf("WriteTo failed: %v", err)
}
}
if err := c.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
}
func testFilePacketConnListen(t *testing.T, net, laddr string) {
switch net {
case "udp", "udp4", "udp6":
laddr += ":0" // any available port
}
l, err := ListenPacket(net, laddr)
if err != nil {
t.Fatalf("ListenPacket failed: %v", err)
}
testFilePacketConn(t, l.(packetConnFile), true)
if err := l.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
}
func testFilePacketConnDial(t *testing.T, net, raddr string) {
switch net {
case "udp", "udp4", "udp6":
raddr += ":12345"
}
c, err := Dial(net, raddr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
testFilePacketConn(t, c.(packetConnFile), false)
if err := c.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
}
var filePacketConnTests = []struct {
net string
addr string
ipv6 bool // test with underlying AF_INET6 socket
linux bool // test with abstract unix domain socket, a Linux-ism
}{
{net: "udp", addr: "127.0.0.1"},
{net: "udp", addr: "[::ffff:127.0.0.1]"},
{net: "udp", addr: "[::1]", ipv6: true},
{net: "udp4", addr: "127.0.0.1"},
{net: "udp4", addr: "[::ffff:127.0.0.1]"},
{net: "udp6", addr: "[::1]", ipv6: true},
{net: "ip4:icmp", addr: "127.0.0.1"},
{net: "unixgram", addr: "@gotest3/net", linux: true},
}
func TestFilePacketConn(t *testing.T) {
switch runtime.GOOS {
case "nacl", "plan9", "windows":
t.Skipf("skipping test on %q", runtime.GOOS)
}
for _, tt := range filePacketConnTests {
if skipServerTest(tt.net, "unixgram", tt.addr, tt.ipv6, false, tt.linux) {
continue
}
if os.Getuid() != 0 && tt.net == "ip4:icmp" {
t.Log("skipping test; must be root")
continue
}
testFilePacketConnListen(t, tt.net, tt.addr)
switch tt.addr {
case "", "0.0.0.0", "[::ffff:0.0.0.0]", "[::]":
default:
if tt.net != "unixgram" {
testFilePacketConnDial(t, tt.net, tt.addr)
}
}
}
}
|