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
|
// Copyright 2012 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.
// This file implements API tests across platforms and will never have a build
// tag.
package net
import (
"io/ioutil"
"os"
"runtime"
"testing"
"time"
)
// testUnixAddr uses ioutil.TempFile to get a name that is unique. It
// also uses /tmp directory in case it is prohibited to create UNIX
// sockets in TMPDIR.
func testUnixAddr() string {
f, err := ioutil.TempFile("/tmp", "nettest")
if err != nil {
panic(err)
}
addr := f.Name()
f.Close()
os.Remove(addr)
return addr
}
var condFatalf = func() func(*testing.T, string, ...interface{}) {
// A few APIs are not implemented yet on both Plan 9 and Windows.
switch runtime.GOOS {
case "plan9", "windows":
return (*testing.T).Logf
}
return (*testing.T).Fatalf
}()
func TestTCPListenerSpecificMethods(t *testing.T) {
switch runtime.GOOS {
case "plan9":
t.Skipf("skipping test on %q", runtime.GOOS)
}
la, err := ResolveTCPAddr("tcp4", "127.0.0.1:0")
if err != nil {
t.Fatalf("ResolveTCPAddr failed: %v", err)
}
ln, err := ListenTCP("tcp4", la)
if err != nil {
t.Fatalf("ListenTCP failed: %v", err)
}
defer ln.Close()
ln.Addr()
ln.SetDeadline(time.Now().Add(30 * time.Nanosecond))
if c, err := ln.Accept(); err != nil {
if !err.(Error).Timeout() {
t.Fatalf("TCPListener.Accept failed: %v", err)
}
} else {
c.Close()
}
if c, err := ln.AcceptTCP(); err != nil {
if !err.(Error).Timeout() {
t.Fatalf("TCPListener.AcceptTCP failed: %v", err)
}
} else {
c.Close()
}
if f, err := ln.File(); err != nil {
condFatalf(t, "TCPListener.File failed: %v", err)
} else {
f.Close()
}
}
func TestTCPConnSpecificMethods(t *testing.T) {
la, err := ResolveTCPAddr("tcp4", "127.0.0.1:0")
if err != nil {
t.Fatalf("ResolveTCPAddr failed: %v", err)
}
ln, err := ListenTCP("tcp4", la)
if err != nil {
t.Fatalf("ListenTCP failed: %v", err)
}
defer ln.Close()
ln.Addr()
done := make(chan int)
go transponder(t, ln, done)
ra, err := ResolveTCPAddr("tcp4", ln.Addr().String())
if err != nil {
t.Fatalf("ResolveTCPAddr failed: %v", err)
}
c, err := DialTCP("tcp4", nil, ra)
if err != nil {
t.Fatalf("DialTCP failed: %v", err)
}
defer c.Close()
c.SetKeepAlive(false)
c.SetKeepAlivePeriod(3 * time.Second)
c.SetLinger(0)
c.SetNoDelay(false)
c.LocalAddr()
c.RemoteAddr()
c.SetDeadline(time.Now().Add(someTimeout))
c.SetReadDeadline(time.Now().Add(someTimeout))
c.SetWriteDeadline(time.Now().Add(someTimeout))
if _, err := c.Write([]byte("TCPCONN TEST")); err != nil {
t.Fatalf("TCPConn.Write failed: %v", err)
}
rb := make([]byte, 128)
if _, err := c.Read(rb); err != nil {
t.Fatalf("TCPConn.Read failed: %v", err)
}
<-done
}
func TestUDPConnSpecificMethods(t *testing.T) {
la, err := ResolveUDPAddr("udp4", "127.0.0.1:0")
if err != nil {
t.Fatalf("ResolveUDPAddr failed: %v", err)
}
c, err := ListenUDP("udp4", la)
if err != nil {
t.Fatalf("ListenUDP failed: %v", err)
}
defer c.Close()
c.LocalAddr()
c.RemoteAddr()
c.SetDeadline(time.Now().Add(someTimeout))
c.SetReadDeadline(time.Now().Add(someTimeout))
c.SetWriteDeadline(time.Now().Add(someTimeout))
c.SetReadBuffer(2048)
c.SetWriteBuffer(2048)
wb := []byte("UDPCONN TEST")
rb := make([]byte, 128)
if _, err := c.WriteToUDP(wb, c.LocalAddr().(*UDPAddr)); err != nil {
t.Fatalf("UDPConn.WriteToUDP failed: %v", err)
}
if _, _, err := c.ReadFromUDP(rb); err != nil {
t.Fatalf("UDPConn.ReadFromUDP failed: %v", err)
}
if _, _, err := c.WriteMsgUDP(wb, nil, c.LocalAddr().(*UDPAddr)); err != nil {
condFatalf(t, "UDPConn.WriteMsgUDP failed: %v", err)
}
if _, _, _, _, err := c.ReadMsgUDP(rb, nil); err != nil {
condFatalf(t, "UDPConn.ReadMsgUDP failed: %v", err)
}
if f, err := c.File(); err != nil {
condFatalf(t, "UDPConn.File failed: %v", err)
} else {
f.Close()
}
defer func() {
if p := recover(); p != nil {
t.Fatalf("UDPConn.WriteToUDP or WriteMsgUDP panicked: %v", p)
}
}()
c.WriteToUDP(wb, nil)
c.WriteMsgUDP(wb, nil, nil)
}
func TestIPConnSpecificMethods(t *testing.T) {
if skip, skipmsg := skipRawSocketTest(t); skip {
t.Skip(skipmsg)
}
la, err := ResolveIPAddr("ip4", "127.0.0.1")
if err != nil {
t.Fatalf("ResolveIPAddr failed: %v", err)
}
c, err := ListenIP("ip4:icmp", la)
if err != nil {
t.Fatalf("ListenIP failed: %v", err)
}
defer c.Close()
c.LocalAddr()
c.RemoteAddr()
c.SetDeadline(time.Now().Add(someTimeout))
c.SetReadDeadline(time.Now().Add(someTimeout))
c.SetWriteDeadline(time.Now().Add(someTimeout))
c.SetReadBuffer(2048)
c.SetWriteBuffer(2048)
wb, err := (&icmpMessage{
Type: icmpv4EchoRequest, Code: 0,
Body: &icmpEcho{
ID: os.Getpid() & 0xffff, Seq: 1,
Data: []byte("IPCONN TEST "),
},
}).Marshal()
if err != nil {
t.Fatalf("icmpMessage.Marshal failed: %v", err)
}
rb := make([]byte, 20+len(wb))
if _, err := c.WriteToIP(wb, c.LocalAddr().(*IPAddr)); err != nil {
t.Fatalf("IPConn.WriteToIP failed: %v", err)
}
if _, _, err := c.ReadFromIP(rb); err != nil {
t.Fatalf("IPConn.ReadFromIP failed: %v", err)
}
if _, _, err := c.WriteMsgIP(wb, nil, c.LocalAddr().(*IPAddr)); err != nil {
condFatalf(t, "IPConn.WriteMsgIP failed: %v", err)
}
if _, _, _, _, err := c.ReadMsgIP(rb, nil); err != nil {
condFatalf(t, "IPConn.ReadMsgIP failed: %v", err)
}
if f, err := c.File(); err != nil {
condFatalf(t, "IPConn.File failed: %v", err)
} else {
f.Close()
}
defer func() {
if p := recover(); p != nil {
t.Fatalf("IPConn.WriteToIP or WriteMsgIP panicked: %v", p)
}
}()
c.WriteToIP(wb, nil)
c.WriteMsgIP(wb, nil, nil)
}
func TestUnixListenerSpecificMethods(t *testing.T) {
switch runtime.GOOS {
case "plan9", "windows":
t.Skipf("skipping test on %q", runtime.GOOS)
}
addr := testUnixAddr()
la, err := ResolveUnixAddr("unix", addr)
if err != nil {
t.Fatalf("ResolveUnixAddr failed: %v", err)
}
ln, err := ListenUnix("unix", la)
if err != nil {
t.Fatalf("ListenUnix failed: %v", err)
}
defer ln.Close()
defer os.Remove(addr)
ln.Addr()
ln.SetDeadline(time.Now().Add(30 * time.Nanosecond))
if c, err := ln.Accept(); err != nil {
if !err.(Error).Timeout() {
t.Fatalf("UnixListener.Accept failed: %v", err)
}
} else {
c.Close()
}
if c, err := ln.AcceptUnix(); err != nil {
if !err.(Error).Timeout() {
t.Fatalf("UnixListener.AcceptUnix failed: %v", err)
}
} else {
c.Close()
}
if f, err := ln.File(); err != nil {
t.Fatalf("UnixListener.File failed: %v", err)
} else {
f.Close()
}
}
func TestUnixConnSpecificMethods(t *testing.T) {
switch runtime.GOOS {
case "plan9", "windows":
t.Skipf("skipping test on %q", runtime.GOOS)
}
addr1, addr2, addr3 := testUnixAddr(), testUnixAddr(), testUnixAddr()
a1, err := ResolveUnixAddr("unixgram", addr1)
if err != nil {
t.Fatalf("ResolveUnixAddr failed: %v", err)
}
c1, err := DialUnix("unixgram", a1, nil)
if err != nil {
t.Fatalf("DialUnix failed: %v", err)
}
defer c1.Close()
defer os.Remove(addr1)
c1.LocalAddr()
c1.RemoteAddr()
c1.SetDeadline(time.Now().Add(someTimeout))
c1.SetReadDeadline(time.Now().Add(someTimeout))
c1.SetWriteDeadline(time.Now().Add(someTimeout))
c1.SetReadBuffer(2048)
c1.SetWriteBuffer(2048)
a2, err := ResolveUnixAddr("unixgram", addr2)
if err != nil {
t.Fatalf("ResolveUnixAddr failed: %v", err)
}
c2, err := DialUnix("unixgram", a2, nil)
if err != nil {
t.Fatalf("DialUnix failed: %v", err)
}
defer c2.Close()
defer os.Remove(addr2)
c2.LocalAddr()
c2.RemoteAddr()
c2.SetDeadline(time.Now().Add(someTimeout))
c2.SetReadDeadline(time.Now().Add(someTimeout))
c2.SetWriteDeadline(time.Now().Add(someTimeout))
c2.SetReadBuffer(2048)
c2.SetWriteBuffer(2048)
a3, err := ResolveUnixAddr("unixgram", addr3)
if err != nil {
t.Fatalf("ResolveUnixAddr failed: %v", err)
}
c3, err := ListenUnixgram("unixgram", a3)
if err != nil {
t.Fatalf("ListenUnixgram failed: %v", err)
}
defer c3.Close()
defer os.Remove(addr3)
c3.LocalAddr()
c3.RemoteAddr()
c3.SetDeadline(time.Now().Add(someTimeout))
c3.SetReadDeadline(time.Now().Add(someTimeout))
c3.SetWriteDeadline(time.Now().Add(someTimeout))
c3.SetReadBuffer(2048)
c3.SetWriteBuffer(2048)
wb := []byte("UNIXCONN TEST")
rb1 := make([]byte, 128)
rb2 := make([]byte, 128)
rb3 := make([]byte, 128)
if _, _, err := c1.WriteMsgUnix(wb, nil, a2); err != nil {
t.Fatalf("UnixConn.WriteMsgUnix failed: %v", err)
}
if _, _, _, _, err := c2.ReadMsgUnix(rb2, nil); err != nil {
t.Fatalf("UnixConn.ReadMsgUnix failed: %v", err)
}
if _, err := c2.WriteToUnix(wb, a1); err != nil {
t.Fatalf("UnixConn.WriteToUnix failed: %v", err)
}
if _, _, err := c1.ReadFromUnix(rb1); err != nil {
t.Fatalf("UnixConn.ReadFromUnix failed: %v", err)
}
if _, err := c3.WriteToUnix(wb, a1); err != nil {
t.Fatalf("UnixConn.WriteToUnix failed: %v", err)
}
if _, _, err := c1.ReadFromUnix(rb1); err != nil {
t.Fatalf("UnixConn.ReadFromUnix failed: %v", err)
}
if _, err := c2.WriteToUnix(wb, a3); err != nil {
t.Fatalf("UnixConn.WriteToUnix failed: %v", err)
}
if _, _, err := c3.ReadFromUnix(rb3); err != nil {
t.Fatalf("UnixConn.ReadFromUnix failed: %v", err)
}
if f, err := c1.File(); err != nil {
t.Fatalf("UnixConn.File failed: %v", err)
} else {
f.Close()
}
defer func() {
if p := recover(); p != nil {
t.Fatalf("UnixConn.WriteToUnix or WriteMsgUnix panicked: %v", p)
}
}()
c1.WriteToUnix(wb, nil)
c1.WriteMsgUnix(wb, nil, nil)
c3.WriteToUnix(wb, nil)
c3.WriteMsgUnix(wb, nil, nil)
}
|