Source file
src/net/server_test.go
1
2
3
4
5
6
7 package net
8
9 import (
10 "os"
11 "testing"
12 )
13
14 var tcpServerTests = []struct {
15 snet, saddr string
16 tnet, taddr string
17 }{
18 {snet: "tcp", saddr: ":0", tnet: "tcp", taddr: "127.0.0.1"},
19 {snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp", taddr: "127.0.0.1"},
20 {snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp", taddr: "127.0.0.1"},
21 {snet: "tcp", saddr: "[::]:0", tnet: "tcp", taddr: "::1"},
22
23 {snet: "tcp", saddr: ":0", tnet: "tcp", taddr: "::1"},
24 {snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp", taddr: "::1"},
25 {snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp", taddr: "::1"},
26 {snet: "tcp", saddr: "[::]:0", tnet: "tcp", taddr: "127.0.0.1"},
27
28 {snet: "tcp", saddr: ":0", tnet: "tcp4", taddr: "127.0.0.1"},
29 {snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp4", taddr: "127.0.0.1"},
30 {snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp4", taddr: "127.0.0.1"},
31 {snet: "tcp", saddr: "[::]:0", tnet: "tcp6", taddr: "::1"},
32
33 {snet: "tcp", saddr: ":0", tnet: "tcp6", taddr: "::1"},
34 {snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp6", taddr: "::1"},
35 {snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp6", taddr: "::1"},
36 {snet: "tcp", saddr: "[::]:0", tnet: "tcp4", taddr: "127.0.0.1"},
37
38 {snet: "tcp", saddr: "127.0.0.1:0", tnet: "tcp", taddr: "127.0.0.1"},
39 {snet: "tcp", saddr: "[::ffff:127.0.0.1]:0", tnet: "tcp", taddr: "127.0.0.1"},
40 {snet: "tcp", saddr: "[::1]:0", tnet: "tcp", taddr: "::1"},
41
42 {snet: "tcp4", saddr: ":0", tnet: "tcp4", taddr: "127.0.0.1"},
43 {snet: "tcp4", saddr: "0.0.0.0:0", tnet: "tcp4", taddr: "127.0.0.1"},
44 {snet: "tcp4", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp4", taddr: "127.0.0.1"},
45
46 {snet: "tcp4", saddr: "127.0.0.1:0", tnet: "tcp4", taddr: "127.0.0.1"},
47
48 {snet: "tcp6", saddr: ":0", tnet: "tcp6", taddr: "::1"},
49 {snet: "tcp6", saddr: "[::]:0", tnet: "tcp6", taddr: "::1"},
50
51 {snet: "tcp6", saddr: "[::1]:0", tnet: "tcp6", taddr: "::1"},
52 }
53
54
55 func TestTCPServer(t *testing.T) {
56 const N = 3
57
58 for i, tt := range tcpServerTests {
59 t.Run(tt.snet+" "+tt.saddr+"<-"+tt.taddr, func(t *testing.T) {
60 if !testableListenArgs(tt.snet, tt.saddr, tt.taddr) {
61 t.Skip("not testable")
62 }
63
64 ln, err := Listen(tt.snet, tt.saddr)
65 if err != nil {
66 if perr := parseDialError(err); perr != nil {
67 t.Error(perr)
68 }
69 t.Fatal(err)
70 }
71
72 var lss []*localServer
73 var tpchs []chan error
74 defer func() {
75 for _, ls := range lss {
76 ls.teardown()
77 }
78 }()
79 for i := 0; i < N; i++ {
80 ls := (&streamListener{Listener: ln}).newLocalServer()
81 lss = append(lss, ls)
82 tpchs = append(tpchs, make(chan error, 1))
83 }
84 for i := 0; i < N; i++ {
85 ch := tpchs[i]
86 handler := func(ls *localServer, ln Listener) { ls.transponder(ln, ch) }
87 if err := lss[i].buildup(handler); err != nil {
88 t.Fatal(err)
89 }
90 }
91
92 var trchs []chan error
93 for i := 0; i < N; i++ {
94 _, port, err := SplitHostPort(lss[i].Listener.Addr().String())
95 if err != nil {
96 t.Fatal(err)
97 }
98 d := Dialer{Timeout: someTimeout}
99 c, err := d.Dial(tt.tnet, JoinHostPort(tt.taddr, port))
100 if err != nil {
101 if perr := parseDialError(err); perr != nil {
102 t.Error(perr)
103 }
104 t.Fatal(err)
105 }
106 defer c.Close()
107 trchs = append(trchs, make(chan error, 1))
108 go transceiver(c, []byte("TCP SERVER TEST"), trchs[i])
109 }
110
111 for _, ch := range trchs {
112 for err := range ch {
113 t.Errorf("#%d: %v", i, err)
114 }
115 }
116 for _, ch := range tpchs {
117 for err := range ch {
118 t.Errorf("#%d: %v", i, err)
119 }
120 }
121 })
122 }
123 }
124
125
126
127 func TestUnixAndUnixpacketServer(t *testing.T) {
128 var unixAndUnixpacketServerTests = []struct {
129 network, address string
130 }{
131 {"unix", testUnixAddr(t)},
132 {"unix", "@nettest/go/unix"},
133
134 {"unixpacket", testUnixAddr(t)},
135 {"unixpacket", "@nettest/go/unixpacket"},
136 }
137
138 const N = 3
139
140 for i, tt := range unixAndUnixpacketServerTests {
141 if !testableListenArgs(tt.network, tt.address, "") {
142 t.Logf("skipping %s test", tt.network+" "+tt.address)
143 continue
144 }
145
146 ln, err := Listen(tt.network, tt.address)
147 if err != nil {
148 if perr := parseDialError(err); perr != nil {
149 t.Error(perr)
150 }
151 t.Fatal(err)
152 }
153
154 var lss []*localServer
155 var tpchs []chan error
156 defer func() {
157 for _, ls := range lss {
158 ls.teardown()
159 }
160 }()
161 for i := 0; i < N; i++ {
162 ls := (&streamListener{Listener: ln}).newLocalServer()
163 lss = append(lss, ls)
164 tpchs = append(tpchs, make(chan error, 1))
165 }
166 for i := 0; i < N; i++ {
167 ch := tpchs[i]
168 handler := func(ls *localServer, ln Listener) { ls.transponder(ln, ch) }
169 if err := lss[i].buildup(handler); err != nil {
170 t.Fatal(err)
171 }
172 }
173
174 var trchs []chan error
175 for i := 0; i < N; i++ {
176 d := Dialer{Timeout: someTimeout}
177 c, err := d.Dial(lss[i].Listener.Addr().Network(), lss[i].Listener.Addr().String())
178 if err != nil {
179 if perr := parseDialError(err); perr != nil {
180 t.Error(perr)
181 }
182 t.Fatal(err)
183 }
184
185 if addr := c.LocalAddr(); addr != nil {
186 t.Logf("connected %s->%s", addr, lss[i].Listener.Addr())
187 }
188
189 defer c.Close()
190 trchs = append(trchs, make(chan error, 1))
191 go transceiver(c, []byte("UNIX AND UNIXPACKET SERVER TEST"), trchs[i])
192 }
193
194 for _, ch := range trchs {
195 for err := range ch {
196 t.Errorf("#%d: %v", i, err)
197 }
198 }
199 for _, ch := range tpchs {
200 for err := range ch {
201 t.Errorf("#%d: %v", i, err)
202 }
203 }
204 }
205 }
206
207 var udpServerTests = []struct {
208 snet, saddr string
209 tnet, taddr string
210 dial bool
211 }{
212 {snet: "udp", saddr: ":0", tnet: "udp", taddr: "127.0.0.1"},
213 {snet: "udp", saddr: "0.0.0.0:0", tnet: "udp", taddr: "127.0.0.1"},
214 {snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp", taddr: "127.0.0.1"},
215 {snet: "udp", saddr: "[::]:0", tnet: "udp", taddr: "::1"},
216
217 {snet: "udp", saddr: ":0", tnet: "udp", taddr: "::1"},
218 {snet: "udp", saddr: "0.0.0.0:0", tnet: "udp", taddr: "::1"},
219 {snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp", taddr: "::1"},
220 {snet: "udp", saddr: "[::]:0", tnet: "udp", taddr: "127.0.0.1"},
221
222 {snet: "udp", saddr: ":0", tnet: "udp4", taddr: "127.0.0.1"},
223 {snet: "udp", saddr: "0.0.0.0:0", tnet: "udp4", taddr: "127.0.0.1"},
224 {snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp4", taddr: "127.0.0.1"},
225 {snet: "udp", saddr: "[::]:0", tnet: "udp6", taddr: "::1"},
226
227 {snet: "udp", saddr: ":0", tnet: "udp6", taddr: "::1"},
228 {snet: "udp", saddr: "0.0.0.0:0", tnet: "udp6", taddr: "::1"},
229 {snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp6", taddr: "::1"},
230 {snet: "udp", saddr: "[::]:0", tnet: "udp4", taddr: "127.0.0.1"},
231
232 {snet: "udp", saddr: "127.0.0.1:0", tnet: "udp", taddr: "127.0.0.1"},
233 {snet: "udp", saddr: "[::ffff:127.0.0.1]:0", tnet: "udp", taddr: "127.0.0.1"},
234 {snet: "udp", saddr: "[::1]:0", tnet: "udp", taddr: "::1"},
235
236 {snet: "udp4", saddr: ":0", tnet: "udp4", taddr: "127.0.0.1"},
237 {snet: "udp4", saddr: "0.0.0.0:0", tnet: "udp4", taddr: "127.0.0.1"},
238 {snet: "udp4", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp4", taddr: "127.0.0.1"},
239
240 {snet: "udp4", saddr: "127.0.0.1:0", tnet: "udp4", taddr: "127.0.0.1"},
241
242 {snet: "udp6", saddr: ":0", tnet: "udp6", taddr: "::1"},
243 {snet: "udp6", saddr: "[::]:0", tnet: "udp6", taddr: "::1"},
244
245 {snet: "udp6", saddr: "[::1]:0", tnet: "udp6", taddr: "::1"},
246
247 {snet: "udp", saddr: "127.0.0.1:0", tnet: "udp", taddr: "127.0.0.1", dial: true},
248
249 {snet: "udp", saddr: "[::1]:0", tnet: "udp", taddr: "::1", dial: true},
250 }
251
252 func TestUDPServer(t *testing.T) {
253 for i, tt := range udpServerTests {
254 if !testableListenArgs(tt.snet, tt.saddr, tt.taddr) {
255 t.Logf("skipping %s test", tt.snet+" "+tt.saddr+"<-"+tt.taddr)
256 continue
257 }
258
259 c1, err := ListenPacket(tt.snet, tt.saddr)
260 if err != nil {
261 if perr := parseDialError(err); perr != nil {
262 t.Error(perr)
263 }
264 t.Fatal(err)
265 }
266
267 ls := (&packetListener{PacketConn: c1}).newLocalServer()
268 defer ls.teardown()
269 tpch := make(chan error, 1)
270 handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, tpch) }
271 if err := ls.buildup(handler); err != nil {
272 t.Fatal(err)
273 }
274
275 trch := make(chan error, 1)
276 _, port, err := SplitHostPort(ls.PacketConn.LocalAddr().String())
277 if err != nil {
278 t.Fatal(err)
279 }
280 if tt.dial {
281 d := Dialer{Timeout: someTimeout}
282 c2, err := d.Dial(tt.tnet, JoinHostPort(tt.taddr, port))
283 if err != nil {
284 if perr := parseDialError(err); perr != nil {
285 t.Error(perr)
286 }
287 t.Fatal(err)
288 }
289 defer c2.Close()
290 go transceiver(c2, []byte("UDP SERVER TEST"), trch)
291 } else {
292 c2, err := ListenPacket(tt.tnet, JoinHostPort(tt.taddr, "0"))
293 if err != nil {
294 if perr := parseDialError(err); perr != nil {
295 t.Error(perr)
296 }
297 t.Fatal(err)
298 }
299 defer c2.Close()
300 dst, err := ResolveUDPAddr(tt.tnet, JoinHostPort(tt.taddr, port))
301 if err != nil {
302 t.Fatal(err)
303 }
304 go packetTransceiver(c2, []byte("UDP SERVER TEST"), dst, trch)
305 }
306
307 for err := range trch {
308 t.Errorf("#%d: %v", i, err)
309 }
310 for err := range tpch {
311 t.Errorf("#%d: %v", i, err)
312 }
313 }
314 }
315
316 func TestUnixgramServer(t *testing.T) {
317 var unixgramServerTests = []struct {
318 saddr string
319 caddr string
320 dial bool
321 }{
322 {saddr: testUnixAddr(t), caddr: testUnixAddr(t)},
323 {saddr: testUnixAddr(t), caddr: testUnixAddr(t), dial: true},
324
325 {saddr: "@nettest/go/unixgram/server", caddr: "@nettest/go/unixgram/client"},
326 }
327
328 for i, tt := range unixgramServerTests {
329 if !testableListenArgs("unixgram", tt.saddr, "") {
330 t.Logf("skipping %s test", "unixgram "+tt.saddr+"<-"+tt.caddr)
331 continue
332 }
333
334 c1, err := ListenPacket("unixgram", tt.saddr)
335 if err != nil {
336 if perr := parseDialError(err); perr != nil {
337 t.Error(perr)
338 }
339 t.Fatal(err)
340 }
341
342 ls := (&packetListener{PacketConn: c1}).newLocalServer()
343 defer ls.teardown()
344 tpch := make(chan error, 1)
345 handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, tpch) }
346 if err := ls.buildup(handler); err != nil {
347 t.Fatal(err)
348 }
349
350 trch := make(chan error, 1)
351 if tt.dial {
352 d := Dialer{Timeout: someTimeout, LocalAddr: &UnixAddr{Net: "unixgram", Name: tt.caddr}}
353 c2, err := d.Dial("unixgram", ls.PacketConn.LocalAddr().String())
354 if err != nil {
355 if perr := parseDialError(err); perr != nil {
356 t.Error(perr)
357 }
358 t.Fatal(err)
359 }
360 defer os.Remove(c2.LocalAddr().String())
361 defer c2.Close()
362 go transceiver(c2, []byte(c2.LocalAddr().String()), trch)
363 } else {
364 c2, err := ListenPacket("unixgram", tt.caddr)
365 if err != nil {
366 if perr := parseDialError(err); perr != nil {
367 t.Error(perr)
368 }
369 t.Fatal(err)
370 }
371 defer os.Remove(c2.LocalAddr().String())
372 defer c2.Close()
373 go packetTransceiver(c2, []byte("UNIXGRAM SERVER TEST"), ls.PacketConn.LocalAddr(), trch)
374 }
375
376 for err := range trch {
377 t.Errorf("#%d: %v", i, err)
378 }
379 for err := range tpch {
380 t.Errorf("#%d: %v", i, err)
381 }
382 }
383 }
384
View as plain text