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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
// Copyright 2009 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 (
"flag"
"io"
"os"
"runtime"
"strconv"
"testing"
"time"
)
func skipServerTest(net, unixsotype, addr string, ipv6, ipv4map, linuxonly bool) bool {
switch runtime.GOOS {
case "linux":
case "plan9", "windows":
// "unix" sockets are not supported on Windows and Plan 9.
if net == unixsotype {
return true
}
default:
if net == unixsotype && linuxonly {
return true
}
}
switch addr {
case "", "0.0.0.0", "[::ffff:0.0.0.0]", "[::]":
if testing.Short() || !*testExternal {
return true
}
}
if ipv6 && !supportsIPv6 {
return true
}
if ipv4map && !supportsIPv4map {
return true
}
return false
}
func tempfile(filename string) string {
// use /tmp in case it is prohibited to create
// UNIX sockets in TMPDIR
return "/tmp/" + filename + "." + strconv.Itoa(os.Getpid())
}
var streamConnServerTests = []struct {
snet string // server side
saddr string
cnet string // client side
caddr string
ipv6 bool // test with underlying AF_INET6 socket
ipv4map bool // test with IPv6 IPv4-mapping functionality
empty bool // test with empty data
linux bool // test with abstract unix domain socket, a Linux-ism
}{
{snet: "tcp", saddr: "", cnet: "tcp", caddr: "127.0.0.1"},
{snet: "tcp", saddr: "0.0.0.0", cnet: "tcp", caddr: "127.0.0.1"},
{snet: "tcp", saddr: "[::ffff:0.0.0.0]", cnet: "tcp", caddr: "127.0.0.1"},
{snet: "tcp", saddr: "[::]", cnet: "tcp", caddr: "[::1]", ipv6: true},
{snet: "tcp", saddr: "", cnet: "tcp", caddr: "[::1]", ipv4map: true},
{snet: "tcp", saddr: "0.0.0.0", cnet: "tcp", caddr: "[::1]", ipv4map: true},
{snet: "tcp", saddr: "[::ffff:0.0.0.0]", cnet: "tcp", caddr: "[::1]", ipv4map: true},
{snet: "tcp", saddr: "[::]", cnet: "tcp", caddr: "127.0.0.1", ipv4map: true},
{snet: "tcp", saddr: "", cnet: "tcp4", caddr: "127.0.0.1"},
{snet: "tcp", saddr: "0.0.0.0", cnet: "tcp4", caddr: "127.0.0.1"},
{snet: "tcp", saddr: "[::ffff:0.0.0.0]", cnet: "tcp4", caddr: "127.0.0.1"},
{snet: "tcp", saddr: "[::]", cnet: "tcp6", caddr: "[::1]", ipv6: true},
{snet: "tcp", saddr: "", cnet: "tcp6", caddr: "[::1]", ipv4map: true},
{snet: "tcp", saddr: "0.0.0.0", cnet: "tcp6", caddr: "[::1]", ipv4map: true},
{snet: "tcp", saddr: "[::ffff:0.0.0.0]", cnet: "tcp6", caddr: "[::1]", ipv4map: true},
{snet: "tcp", saddr: "[::]", cnet: "tcp4", caddr: "127.0.0.1", ipv4map: true},
{snet: "tcp", saddr: "127.0.0.1", cnet: "tcp", caddr: "127.0.0.1"},
{snet: "tcp", saddr: "[::ffff:127.0.0.1]", cnet: "tcp", caddr: "127.0.0.1"},
{snet: "tcp", saddr: "[::1]", cnet: "tcp", caddr: "[::1]", ipv6: true},
{snet: "tcp4", saddr: "", cnet: "tcp4", caddr: "127.0.0.1"},
{snet: "tcp4", saddr: "0.0.0.0", cnet: "tcp4", caddr: "127.0.0.1"},
{snet: "tcp4", saddr: "[::ffff:0.0.0.0]", cnet: "tcp4", caddr: "127.0.0.1"},
{snet: "tcp4", saddr: "127.0.0.1", cnet: "tcp4", caddr: "127.0.0.1"},
{snet: "tcp6", saddr: "", cnet: "tcp6", caddr: "[::1]", ipv6: true},
{snet: "tcp6", saddr: "[::]", cnet: "tcp6", caddr: "[::1]", ipv6: true},
{snet: "tcp6", saddr: "[::1]", cnet: "tcp6", caddr: "[::1]", ipv6: true},
{snet: "unix", saddr: tempfile("gotest1.net"), cnet: "unix", caddr: tempfile("gotest1.net.local")},
{snet: "unix", saddr: "@gotest2/net", cnet: "unix", caddr: "@gotest2/net.local", linux: true},
}
func TestStreamConnServer(t *testing.T) {
for _, tt := range streamConnServerTests {
if skipServerTest(tt.snet, "unix", tt.saddr, tt.ipv6, tt.ipv4map, tt.linux) {
continue
}
listening := make(chan string)
done := make(chan int)
switch tt.snet {
case "tcp", "tcp4", "tcp6":
tt.saddr += ":0"
case "unix":
os.Remove(tt.saddr)
os.Remove(tt.caddr)
}
go runStreamConnServer(t, tt.snet, tt.saddr, listening, done)
taddr := <-listening // wait for server to start
switch tt.cnet {
case "tcp", "tcp4", "tcp6":
_, port, err := SplitHostPort(taddr)
if err != nil {
t.Fatalf("SplitHostPort(%q) failed: %v", taddr, err)
}
taddr = tt.caddr + ":" + port
}
runStreamConnClient(t, tt.cnet, taddr, tt.empty)
<-done // make sure server stopped
switch tt.snet {
case "unix":
os.Remove(tt.saddr)
os.Remove(tt.caddr)
}
}
}
var seqpacketConnServerTests = []struct {
net string
saddr string // server address
caddr string // client address
empty bool // test with empty data
}{
{net: "unixpacket", saddr: tempfile("/gotest3.net"), caddr: tempfile("gotest3.net.local")},
{net: "unixpacket", saddr: "@gotest4/net", caddr: "@gotest4/net.local"},
}
func TestSeqpacketConnServer(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skipf("skipping test on %q", runtime.GOOS)
}
for _, tt := range seqpacketConnServerTests {
listening := make(chan string)
done := make(chan int)
switch tt.net {
case "unixpacket":
os.Remove(tt.saddr)
os.Remove(tt.caddr)
}
go runStreamConnServer(t, tt.net, tt.saddr, listening, done)
taddr := <-listening // wait for server to start
runStreamConnClient(t, tt.net, taddr, tt.empty)
<-done // make sure server stopped
switch tt.net {
case "unixpacket":
os.Remove(tt.saddr)
os.Remove(tt.caddr)
}
}
}
func runStreamConnServer(t *testing.T, net, laddr string, listening chan<- string, done chan<- int) {
defer close(done)
l, err := Listen(net, laddr)
if err != nil {
t.Errorf("Listen(%q, %q) failed: %v", net, laddr, err)
listening <- "<nil>"
return
}
defer l.Close()
listening <- l.Addr().String()
echo := func(rw io.ReadWriter, done chan<- int) {
buf := make([]byte, 1024)
for {
n, err := rw.Read(buf[0:])
if err != nil || n == 0 || string(buf[:n]) == "END" {
break
}
rw.Write(buf[0:n])
}
close(done)
}
run:
for {
c, err := l.Accept()
if err != nil {
t.Logf("Accept failed: %v", err)
continue run
}
echodone := make(chan int)
go echo(c, echodone)
<-echodone // make sure echo stopped
c.Close()
break run
}
}
func runStreamConnClient(t *testing.T, net, taddr string, isEmpty bool) {
c, err := Dial(net, taddr)
if err != nil {
t.Fatalf("Dial(%q, %q) failed: %v", net, taddr, err)
}
defer c.Close()
c.SetReadDeadline(time.Now().Add(1 * time.Second))
var wb []byte
if !isEmpty {
wb = []byte("StreamConnClient by Dial\n")
}
if n, err := c.Write(wb); err != nil || n != len(wb) {
t.Fatalf("Write failed: %v, %v; want %v, <nil>", n, err, len(wb))
}
rb := make([]byte, 1024)
if n, err := c.Read(rb[0:]); err != nil || n != len(wb) {
t.Fatalf("Read failed: %v, %v; want %v, <nil>", n, err, len(wb))
}
// Send explicit ending for unixpacket.
// Older Linux kernels do not stop reads on close.
switch net {
case "unixpacket":
c.Write([]byte("END"))
}
}
// Do not test empty datagrams by default.
// It causes unexplained timeouts on some systems,
// including Snow Leopard. I think that the kernel
// doesn't quite expect them.
var testDatagram = flag.Bool("datagram", false, "whether to test udp and unixgram")
var datagramPacketConnServerTests = []struct {
snet string // server side
saddr string
cnet string // client side
caddr string
ipv6 bool // test with underlying AF_INET6 socket
ipv4map bool // test with IPv6 IPv4-mapping functionality
dial bool // test with Dial or DialUnix
empty bool // test with empty data
linux bool // test with abstract unix domain socket, a Linux-ism
}{
{snet: "udp", saddr: "", cnet: "udp", caddr: "127.0.0.1"},
{snet: "udp", saddr: "0.0.0.0", cnet: "udp", caddr: "127.0.0.1"},
{snet: "udp", saddr: "[::ffff:0.0.0.0]", cnet: "udp", caddr: "127.0.0.1"},
{snet: "udp", saddr: "[::]", cnet: "udp", caddr: "[::1]", ipv6: true},
{snet: "udp", saddr: "", cnet: "udp", caddr: "[::1]", ipv4map: true},
{snet: "udp", saddr: "0.0.0.0", cnet: "udp", caddr: "[::1]", ipv4map: true},
{snet: "udp", saddr: "[::ffff:0.0.0.0]", cnet: "udp", caddr: "[::1]", ipv4map: true},
{snet: "udp", saddr: "[::]", cnet: "udp", caddr: "127.0.0.1", ipv4map: true},
{snet: "udp", saddr: "", cnet: "udp4", caddr: "127.0.0.1"},
{snet: "udp", saddr: "0.0.0.0", cnet: "udp4", caddr: "127.0.0.1"},
{snet: "udp", saddr: "[::ffff:0.0.0.0]", cnet: "udp4", caddr: "127.0.0.1"},
{snet: "udp", saddr: "[::]", cnet: "udp6", caddr: "[::1]", ipv6: true},
{snet: "udp", saddr: "", cnet: "udp6", caddr: "[::1]", ipv4map: true},
{snet: "udp", saddr: "0.0.0.0", cnet: "udp6", caddr: "[::1]", ipv4map: true},
{snet: "udp", saddr: "[::ffff:0.0.0.0]", cnet: "udp6", caddr: "[::1]", ipv4map: true},
{snet: "udp", saddr: "[::]", cnet: "udp4", caddr: "127.0.0.1", ipv4map: true},
{snet: "udp", saddr: "127.0.0.1", cnet: "udp", caddr: "127.0.0.1"},
{snet: "udp", saddr: "[::ffff:127.0.0.1]", cnet: "udp", caddr: "127.0.0.1"},
{snet: "udp", saddr: "[::1]", cnet: "udp", caddr: "[::1]", ipv6: true},
{snet: "udp4", saddr: "", cnet: "udp4", caddr: "127.0.0.1"},
{snet: "udp4", saddr: "0.0.0.0", cnet: "udp4", caddr: "127.0.0.1"},
{snet: "udp4", saddr: "[::ffff:0.0.0.0]", cnet: "udp4", caddr: "127.0.0.1"},
{snet: "udp4", saddr: "127.0.0.1", cnet: "udp4", caddr: "127.0.0.1"},
{snet: "udp6", saddr: "", cnet: "udp6", caddr: "[::1]", ipv6: true},
{snet: "udp6", saddr: "[::]", cnet: "udp6", caddr: "[::1]", ipv6: true},
{snet: "udp6", saddr: "[::1]", cnet: "udp6", caddr: "[::1]", ipv6: true},
{snet: "udp", saddr: "127.0.0.1", cnet: "udp", caddr: "127.0.0.1", dial: true},
{snet: "udp", saddr: "127.0.0.1", cnet: "udp", caddr: "127.0.0.1", empty: true},
{snet: "udp", saddr: "127.0.0.1", cnet: "udp", caddr: "127.0.0.1", dial: true, empty: true},
{snet: "udp", saddr: "[::1]", cnet: "udp", caddr: "[::1]", ipv6: true, dial: true},
{snet: "udp", saddr: "[::1]", cnet: "udp", caddr: "[::1]", ipv6: true, empty: true},
{snet: "udp", saddr: "[::1]", cnet: "udp", caddr: "[::1]", ipv6: true, dial: true, empty: true},
{snet: "unixgram", saddr: tempfile("gotest5.net"), cnet: "unixgram", caddr: tempfile("gotest5.net.local")},
{snet: "unixgram", saddr: tempfile("gotest5.net"), cnet: "unixgram", caddr: tempfile("gotest5.net.local"), dial: true},
{snet: "unixgram", saddr: tempfile("gotest5.net"), cnet: "unixgram", caddr: tempfile("gotest5.net.local"), empty: true},
{snet: "unixgram", saddr: tempfile("gotest5.net"), cnet: "unixgram", caddr: tempfile("gotest5.net.local"), dial: true, empty: true},
{snet: "unixgram", saddr: "@gotest6/net", cnet: "unixgram", caddr: "@gotest6/net.local", linux: true},
}
func TestDatagramPacketConnServer(t *testing.T) {
if !*testDatagram {
return
}
for _, tt := range datagramPacketConnServerTests {
if skipServerTest(tt.snet, "unixgram", tt.saddr, tt.ipv6, tt.ipv4map, tt.linux) {
continue
}
listening := make(chan string)
done := make(chan int)
switch tt.snet {
case "udp", "udp4", "udp6":
tt.saddr += ":0"
case "unixgram":
os.Remove(tt.saddr)
os.Remove(tt.caddr)
}
go runDatagramPacketConnServer(t, tt.snet, tt.saddr, listening, done)
taddr := <-listening // wait for server to start
switch tt.cnet {
case "udp", "udp4", "udp6":
_, port, err := SplitHostPort(taddr)
if err != nil {
t.Fatalf("SplitHostPort(%q) failed: %v", taddr, err)
}
taddr = tt.caddr + ":" + port
tt.caddr += ":0"
}
if tt.dial {
runDatagramConnClient(t, tt.cnet, tt.caddr, taddr, tt.empty)
} else {
runDatagramPacketConnClient(t, tt.cnet, tt.caddr, taddr, tt.empty)
}
<-done // tell server to stop
<-done // make sure server stopped
switch tt.snet {
case "unixgram":
os.Remove(tt.saddr)
os.Remove(tt.caddr)
}
}
}
func runDatagramPacketConnServer(t *testing.T, net, laddr string, listening chan<- string, done chan<- int) {
c, err := ListenPacket(net, laddr)
if err != nil {
t.Errorf("ListenPacket(%q, %q) failed: %v", net, laddr, err)
listening <- "<nil>"
done <- 1
return
}
defer c.Close()
listening <- c.LocalAddr().String()
buf := make([]byte, 1024)
run:
for {
c.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
n, ra, err := c.ReadFrom(buf[0:])
if nerr, ok := err.(Error); ok && nerr.Timeout() {
select {
case done <- 1:
break run
default:
continue run
}
}
if err != nil {
break run
}
if _, err = c.WriteTo(buf[0:n], ra); err != nil {
t.Errorf("WriteTo(%v) failed: %v", ra, err)
break run
}
}
done <- 1
}
func runDatagramConnClient(t *testing.T, net, laddr, taddr string, isEmpty bool) {
var c Conn
var err error
switch net {
case "udp", "udp4", "udp6":
c, err = Dial(net, taddr)
if err != nil {
t.Fatalf("Dial(%q, %q) failed: %v", net, taddr, err)
}
case "unixgram":
c, err = DialUnix(net, &UnixAddr{Name: laddr, Net: net}, &UnixAddr{Name: taddr, Net: net})
if err != nil {
t.Fatalf("DialUnix(%q, {%q, %q}) failed: %v", net, laddr, taddr, err)
}
}
defer c.Close()
c.SetReadDeadline(time.Now().Add(1 * time.Second))
var wb []byte
if !isEmpty {
wb = []byte("DatagramConnClient by Dial\n")
}
if n, err := c.Write(wb[0:]); err != nil || n != len(wb) {
t.Fatalf("Write failed: %v, %v; want %v, <nil>", n, err, len(wb))
}
rb := make([]byte, 1024)
if n, err := c.Read(rb[0:]); err != nil || n != len(wb) {
t.Fatalf("Read failed: %v, %v; want %v, <nil>", n, err, len(wb))
}
}
func runDatagramPacketConnClient(t *testing.T, net, laddr, taddr string, isEmpty bool) {
var ra Addr
var err error
switch net {
case "udp", "udp4", "udp6":
ra, err = ResolveUDPAddr(net, taddr)
if err != nil {
t.Fatalf("ResolveUDPAddr(%q, %q) failed: %v", net, taddr, err)
}
case "unixgram":
ra, err = ResolveUnixAddr(net, taddr)
if err != nil {
t.Fatalf("ResolveUxixAddr(%q, %q) failed: %v", net, taddr, err)
}
}
c, err := ListenPacket(net, laddr)
if err != nil {
t.Fatalf("ListenPacket(%q, %q) faild: %v", net, laddr, err)
}
defer c.Close()
c.SetReadDeadline(time.Now().Add(1 * time.Second))
var wb []byte
if !isEmpty {
wb = []byte("DatagramPacketConnClient by ListenPacket\n")
}
if n, err := c.WriteTo(wb[0:], ra); err != nil || n != len(wb) {
t.Fatalf("WriteTo(%v) failed: %v, %v; want %v, <nil>", ra, n, err, len(wb))
}
rb := make([]byte, 1024)
if n, _, err := c.ReadFrom(rb[0:]); err != nil || n != len(wb) {
t.Fatalf("ReadFrom failed: %v, %v; want %v, <nil>", n, err, len(wb))
}
}
|