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
|
// 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 (
"io"
"io/ioutil"
"os"
"runtime"
"testing"
"time"
)
func TestShutdown(t *testing.T) {
if runtime.GOOS == "plan9" {
t.Skipf("skipping test on %q", runtime.GOOS)
}
ln, err := Listen("tcp", "127.0.0.1:0")
if err != nil {
if ln, err = Listen("tcp6", "[::1]:0"); err != nil {
t.Fatalf("ListenTCP on :0: %v", err)
}
}
go func() {
defer ln.Close()
c, err := ln.Accept()
if err != nil {
t.Errorf("Accept: %v", err)
return
}
var buf [10]byte
n, err := c.Read(buf[:])
if n != 0 || err != io.EOF {
t.Errorf("server Read = %d, %v; want 0, io.EOF", n, err)
return
}
c.Write([]byte("response"))
c.Close()
}()
c, err := Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatalf("Dial: %v", err)
}
defer c.Close()
err = c.(*TCPConn).CloseWrite()
if err != nil {
t.Fatalf("CloseWrite: %v", err)
}
var buf [10]byte
n, err := c.Read(buf[:])
if err != nil {
t.Fatalf("client Read: %d, %v", n, err)
}
got := string(buf[:n])
if got != "response" {
t.Errorf("read = %q, want \"response\"", got)
}
}
func TestShutdownUnix(t *testing.T) {
switch runtime.GOOS {
case "nacl", "plan9", "windows":
t.Skipf("skipping test on %q", runtime.GOOS)
}
f, err := ioutil.TempFile("", "go_net_unixtest")
if err != nil {
t.Fatalf("TempFile: %s", err)
}
f.Close()
tmpname := f.Name()
os.Remove(tmpname)
ln, err := Listen("unix", tmpname)
if err != nil {
t.Fatalf("ListenUnix on %s: %s", tmpname, err)
}
defer func() {
ln.Close()
os.Remove(tmpname)
}()
go func() {
c, err := ln.Accept()
if err != nil {
t.Errorf("Accept: %v", err)
return
}
var buf [10]byte
n, err := c.Read(buf[:])
if n != 0 || err != io.EOF {
t.Errorf("server Read = %d, %v; want 0, io.EOF", n, err)
return
}
c.Write([]byte("response"))
c.Close()
}()
c, err := Dial("unix", tmpname)
if err != nil {
t.Fatalf("Dial: %v", err)
}
defer c.Close()
err = c.(*UnixConn).CloseWrite()
if err != nil {
t.Fatalf("CloseWrite: %v", err)
}
var buf [10]byte
n, err := c.Read(buf[:])
if err != nil {
t.Fatalf("client Read: %d, %v", n, err)
}
got := string(buf[:n])
if got != "response" {
t.Errorf("read = %q, want \"response\"", got)
}
}
func TestTCPListenClose(t *testing.T) {
ln, err := Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
done := make(chan bool, 1)
go func() {
time.Sleep(100 * time.Millisecond)
ln.Close()
}()
go func() {
c, err := ln.Accept()
if err == nil {
c.Close()
t.Error("Accept succeeded")
} else {
t.Logf("Accept timeout error: %s (any error is fine)", err)
}
done <- true
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("timeout waiting for TCP close")
}
}
func TestUDPListenClose(t *testing.T) {
switch runtime.GOOS {
case "plan9":
t.Skipf("skipping test on %q", runtime.GOOS)
}
ln, err := ListenPacket("udp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
buf := make([]byte, 1000)
done := make(chan bool, 1)
go func() {
time.Sleep(100 * time.Millisecond)
ln.Close()
}()
go func() {
_, _, err = ln.ReadFrom(buf)
if err == nil {
t.Error("ReadFrom succeeded")
} else {
t.Logf("ReadFrom timeout error: %s (any error is fine)", err)
}
done <- true
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("timeout waiting for UDP close")
}
}
func TestTCPClose(t *testing.T) {
switch runtime.GOOS {
case "plan9":
t.Skipf("skipping test on %q", runtime.GOOS)
}
l, err := Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
read := func(r io.Reader) error {
var m [1]byte
_, err := r.Read(m[:])
return err
}
go func() {
c, err := Dial("tcp", l.Addr().String())
if err != nil {
t.Errorf("Dial: %v", err)
return
}
go read(c)
time.Sleep(10 * time.Millisecond)
c.Close()
}()
c, err := l.Accept()
if err != nil {
t.Fatal(err)
}
defer c.Close()
for err == nil {
err = read(c)
}
if err != nil && err != io.EOF {
t.Fatal(err)
}
}
func TestErrorNil(t *testing.T) {
c, err := Dial("tcp", "127.0.0.1:65535")
if err == nil {
t.Fatal("Dial 127.0.0.1:65535 succeeded")
}
if c != nil {
t.Fatalf("Dial returned non-nil interface %T(%v) with err != nil", c, c)
}
// Make Listen fail by relistening on the same address.
l, err := Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen 127.0.0.1:0: %v", err)
}
defer l.Close()
l1, err := Listen("tcp", l.Addr().String())
if err == nil {
t.Fatalf("second Listen %v: %v", l.Addr(), err)
}
if l1 != nil {
t.Fatalf("Listen returned non-nil interface %T(%v) with err != nil", l1, l1)
}
// Make ListenPacket fail by relistening on the same address.
lp, err := ListenPacket("udp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen 127.0.0.1:0: %v", err)
}
defer lp.Close()
lp1, err := ListenPacket("udp", lp.LocalAddr().String())
if err == nil {
t.Fatalf("second Listen %v: %v", lp.LocalAddr(), err)
}
if lp1 != nil {
t.Fatalf("ListenPacket returned non-nil interface %T(%v) with err != nil", lp1, lp1)
}
}
|