-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathconn_close_test.go
282 lines (245 loc) · 8.31 KB
/
conn_close_test.go
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
// Copyright 2023 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.
//go:build go1.21
package quic
import (
"context"
"crypto/tls"
"errors"
"testing"
"time"
)
func TestConnCloseResponseBackoff(t *testing.T) {
tc := newTestConn(t, clientSide, func(c *Config) {
clear(c.StatelessResetKey[:])
})
tc.handshake()
tc.conn.Abort(nil)
tc.wantFrame("aborting connection generates CONN_CLOSE",
packetType1RTT, debugFrameConnectionCloseTransport{
code: errNo,
})
waiting := runAsync(tc, func(ctx context.Context) (struct{}, error) {
return struct{}{}, tc.conn.Wait(ctx)
})
if _, err := waiting.result(); err != errNotDone {
t.Errorf("conn.Wait() = %v, want still waiting", err)
}
tc.writeFrames(packetType1RTT, debugFramePing{})
tc.wantIdle("packets received immediately after CONN_CLOSE receive no response")
tc.advance(1100 * time.Microsecond)
tc.writeFrames(packetType1RTT, debugFramePing{})
tc.wantFrame("receiving packet 1.1ms after CONN_CLOSE generates another CONN_CLOSE",
packetType1RTT, debugFrameConnectionCloseTransport{
code: errNo,
})
tc.advance(1100 * time.Microsecond)
tc.writeFrames(packetType1RTT, debugFramePing{})
tc.wantIdle("no response to packet, because CONN_CLOSE backoff is now 2ms")
tc.advance(1000 * time.Microsecond)
tc.writeFrames(packetType1RTT, debugFramePing{})
tc.wantFrame("2ms since last CONN_CLOSE, receiving a packet generates another CONN_CLOSE",
packetType1RTT, debugFrameConnectionCloseTransport{
code: errNo,
})
if _, err := waiting.result(); err != errNotDone {
t.Errorf("conn.Wait() = %v, want still waiting", err)
}
tc.advance(100000 * time.Microsecond)
tc.writeFrames(packetType1RTT, debugFramePing{})
tc.wantIdle("drain timer expired, no more responses")
if _, err := waiting.result(); !errors.Is(err, errNoPeerResponse) {
t.Errorf("blocked conn.Wait() = %v, want errNoPeerResponse", err)
}
if err := tc.conn.Wait(canceledContext()); !errors.Is(err, errNoPeerResponse) {
t.Errorf("non-blocking conn.Wait() = %v, want errNoPeerResponse", err)
}
}
func TestConnCloseWithPeerResponse(t *testing.T) {
qr := &qlogRecord{}
tc := newTestConn(t, clientSide, qr.config)
tc.handshake()
tc.conn.Abort(nil)
tc.wantFrame("aborting connection generates CONN_CLOSE",
packetType1RTT, debugFrameConnectionCloseTransport{
code: errNo,
})
waiting := runAsync(tc, func(ctx context.Context) (struct{}, error) {
return struct{}{}, tc.conn.Wait(ctx)
})
if _, err := waiting.result(); err != errNotDone {
t.Errorf("conn.Wait() = %v, want still waiting", err)
}
tc.writeFrames(packetType1RTT, debugFrameConnectionCloseApplication{
code: 20,
})
wantErr := &ApplicationError{
Code: 20,
}
if _, err := waiting.result(); !errors.Is(err, wantErr) {
t.Errorf("blocked conn.Wait() = %v, want %v", err, wantErr)
}
if err := tc.conn.Wait(canceledContext()); !errors.Is(err, wantErr) {
t.Errorf("non-blocking conn.Wait() = %v, want %v", err, wantErr)
}
tc.advance(1 * time.Second) // long enough to exit the draining state
qr.wantEvents(t, jsonEvent{
"name": "connectivity:connection_closed",
"data": map[string]any{
"trigger": "application",
},
})
}
func TestConnClosePeerCloses(t *testing.T) {
qr := &qlogRecord{}
tc := newTestConn(t, clientSide, qr.config)
tc.handshake()
wantErr := &ApplicationError{
Code: 42,
Reason: "why?",
}
tc.writeFrames(packetType1RTT, debugFrameConnectionCloseApplication{
code: wantErr.Code,
reason: wantErr.Reason,
})
tc.wantIdle("CONN_CLOSE response not sent until user closes this side")
if err := tc.conn.Wait(canceledContext()); !errors.Is(err, wantErr) {
t.Errorf("conn.Wait() = %v, want %v", err, wantErr)
}
tc.conn.Abort(&ApplicationError{
Code: 9,
Reason: "because",
})
tc.wantFrame("CONN_CLOSE sent after user closes connection",
packetType1RTT, debugFrameConnectionCloseApplication{
code: 9,
reason: "because",
})
tc.advance(1 * time.Second) // long enough to exit the draining state
qr.wantEvents(t, jsonEvent{
"name": "connectivity:connection_closed",
"data": map[string]any{
"trigger": "application",
},
})
}
func TestConnCloseReceiveInInitial(t *testing.T) {
tc := newTestConn(t, clientSide)
tc.wantFrame("client sends Initial CRYPTO frame",
packetTypeInitial, debugFrameCrypto{
data: tc.cryptoDataOut[tls.QUICEncryptionLevelInitial],
})
tc.writeFrames(packetTypeInitial, debugFrameConnectionCloseTransport{
code: errConnectionRefused,
})
tc.wantIdle("CONN_CLOSE response not sent until user closes this side")
wantErr := peerTransportError{code: errConnectionRefused}
if err := tc.conn.Wait(canceledContext()); !errors.Is(err, wantErr) {
t.Errorf("conn.Wait() = %v, want %v", err, wantErr)
}
tc.conn.Abort(&ApplicationError{Code: 1})
tc.wantFrame("CONN_CLOSE in Initial frame is APPLICATION_ERROR",
packetTypeInitial, debugFrameConnectionCloseTransport{
code: errApplicationError,
})
tc.wantIdle("no more frames to send")
}
func TestConnCloseReceiveInHandshake(t *testing.T) {
tc := newTestConn(t, clientSide)
tc.ignoreFrame(frameTypeAck)
tc.wantFrame("client sends Initial CRYPTO frame",
packetTypeInitial, debugFrameCrypto{
data: tc.cryptoDataOut[tls.QUICEncryptionLevelInitial],
})
tc.writeFrames(packetTypeInitial, debugFrameCrypto{
data: tc.cryptoDataIn[tls.QUICEncryptionLevelInitial],
})
tc.writeFrames(packetTypeHandshake, debugFrameConnectionCloseTransport{
code: errConnectionRefused,
})
tc.wantIdle("CONN_CLOSE response not sent until user closes this side")
wantErr := peerTransportError{code: errConnectionRefused}
if err := tc.conn.Wait(canceledContext()); !errors.Is(err, wantErr) {
t.Errorf("conn.Wait() = %v, want %v", err, wantErr)
}
// The conn has Initial and Handshake keys, so it will send CONN_CLOSE in both spaces.
tc.conn.Abort(&ApplicationError{Code: 1})
tc.wantFrame("CONN_CLOSE in Initial frame is APPLICATION_ERROR",
packetTypeInitial, debugFrameConnectionCloseTransport{
code: errApplicationError,
})
tc.wantFrame("CONN_CLOSE in Handshake frame is APPLICATION_ERROR",
packetTypeHandshake, debugFrameConnectionCloseTransport{
code: errApplicationError,
})
tc.wantIdle("no more frames to send")
}
func TestConnCloseClosedByEndpoint(t *testing.T) {
ctx := canceledContext()
tc := newTestConn(t, clientSide)
tc.handshake()
tc.endpoint.e.Close(ctx)
tc.wantFrame("endpoint closes connection before exiting",
packetType1RTT, debugFrameConnectionCloseTransport{
code: errNo,
})
}
func testConnCloseUnblocks(t *testing.T, f func(context.Context, *testConn) error, opts ...any) {
tc := newTestConn(t, clientSide, opts...)
tc.handshake()
op := runAsync(tc, func(ctx context.Context) (struct{}, error) {
return struct{}{}, f(ctx, tc)
})
if _, err := op.result(); err != errNotDone {
t.Fatalf("before abort, op = %v, want errNotDone", err)
}
tc.conn.Abort(nil)
if _, err := op.result(); err == nil || err == errNotDone {
t.Fatalf("after abort, op = %v, want error", err)
}
}
func TestConnCloseUnblocksAcceptStream(t *testing.T) {
testConnCloseUnblocks(t, func(ctx context.Context, tc *testConn) error {
_, err := tc.conn.AcceptStream(ctx)
return err
}, permissiveTransportParameters)
}
func TestConnCloseUnblocksNewStream(t *testing.T) {
testConnCloseUnblocks(t, func(ctx context.Context, tc *testConn) error {
_, err := tc.conn.NewStream(ctx)
return err
})
}
func TestConnCloseUnblocksStreamRead(t *testing.T) {
testConnCloseUnblocks(t, func(ctx context.Context, tc *testConn) error {
s := newLocalStream(t, tc, bidiStream)
s.SetReadContext(ctx)
buf := make([]byte, 16)
_, err := s.Read(buf)
return err
}, permissiveTransportParameters)
}
func TestConnCloseUnblocksStreamWrite(t *testing.T) {
testConnCloseUnblocks(t, func(ctx context.Context, tc *testConn) error {
s := newLocalStream(t, tc, bidiStream)
s.SetWriteContext(ctx)
buf := make([]byte, 32)
_, err := s.Write(buf)
return err
}, permissiveTransportParameters, func(c *Config) {
c.MaxStreamWriteBufferSize = 16
})
}
func TestConnCloseUnblocksStreamClose(t *testing.T) {
testConnCloseUnblocks(t, func(ctx context.Context, tc *testConn) error {
s := newLocalStream(t, tc, bidiStream)
s.SetWriteContext(ctx)
buf := make([]byte, 16)
_, err := s.Write(buf)
if err != nil {
return err
}
return s.Close()
}, permissiveTransportParameters)
}