-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
server_test.go
311 lines (274 loc) · 10 KB
/
server_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
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
package webtransport_test
import (
"context"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
"github.com/quic-go/webtransport-go"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
"github.com/quic-go/quic-go/quicvarint"
"github.com/stretchr/testify/require"
)
const webTransportFrameType = 0x41
func scaleDuration(d time.Duration) time.Duration {
if os.Getenv("CI") != "" {
return 5 * d
}
return d
}
func TestUpgradeFailures(t *testing.T) {
var s webtransport.Server
t.Run("wrong request method", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/webtransport", nil)
_, err := s.Upgrade(httptest.NewRecorder(), req)
require.EqualError(t, err, "expected CONNECT request, got GET")
})
t.Run("wrong protocol", func(t *testing.T) {
req := httptest.NewRequest(http.MethodConnect, "/webtransport", nil)
_, err := s.Upgrade(httptest.NewRecorder(), req)
require.EqualError(t, err, "unexpected protocol: HTTP/1.1")
})
t.Run("missing WebTransport header", func(t *testing.T) {
req := httptest.NewRequest(http.MethodConnect, "/webtransport", nil)
req.Proto = "webtransport"
_, err := s.Upgrade(httptest.NewRecorder(), req)
require.EqualError(t, err, "missing or invalid Sec-Webtransport-Http3-Draft02 header")
})
}
func newWebTransportRequest(t *testing.T, addr string) *http.Request {
t.Helper()
u, err := url.Parse(addr)
require.NoError(t, err)
hdr := make(http.Header)
hdr.Add("Sec-Webtransport-Http3-Draft02", "1")
return &http.Request{
Method: http.MethodConnect,
Header: hdr,
Proto: "webtransport",
Host: u.Host,
URL: u,
}
}
func createStreamAndWrite(t *testing.T, conn quic.Connection, sessionID uint64, data []byte) quic.Stream {
t.Helper()
str, err := conn.OpenStream()
require.NoError(t, err)
var buf []byte
buf = quicvarint.Append(buf, webTransportFrameType)
buf = quicvarint.Append(buf, sessionID) // stream ID of the stream used to establish the WebTransport session.
buf = append(buf, data...)
_, err = str.Write(buf)
require.NoError(t, err)
require.NoError(t, str.Close())
return str
}
func TestServerReorderedUpgradeRequest(t *testing.T) {
s := webtransport.Server{
H3: http3.Server{TLSConfig: tlsConf},
}
defer s.Close()
connChan := make(chan *webtransport.Session)
addHandler(t, &s, func(c *webtransport.Session) {
connChan <- c
})
udpConn, err := net.ListenUDP("udp", nil)
require.NoError(t, err)
port := udpConn.LocalAddr().(*net.UDPAddr).Port
go s.Serve(udpConn)
cconn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", port),
&tls.Config{RootCAs: certPool, NextProtos: []string{http3.NextProtoH3}},
&quic.Config{EnableDatagrams: true},
)
require.NoError(t, err)
// Open a new stream for a WebTransport session we'll establish later. Stream ID: 0.
createStreamAndWrite(t, cconn, 4, []byte("foobar"))
tr := &http3.Transport{EnableDatagrams: true}
conn := tr.NewClientConn(cconn)
// make sure this request actually arrives first
time.Sleep(scaleDuration(50 * time.Millisecond))
// Create a new WebTransport session. Stream ID: 4.
str, err := conn.OpenRequestStream(context.Background())
require.NoError(t, err)
require.NoError(t, str.SendRequestHeader(newWebTransportRequest(t, fmt.Sprintf("https://localhost:%d/webtransport", port))))
rsp, err := str.ReadResponse()
require.NoError(t, err)
require.Equal(t, http.StatusOK, rsp.StatusCode)
sconn := <-connChan
defer sconn.CloseWithError(0, "")
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
sstr, err := sconn.AcceptStream(ctx)
require.NoError(t, err)
data, err := io.ReadAll(sstr)
require.NoError(t, err)
require.Equal(t, []byte("foobar"), data)
// Establish another stream and make sure it's accepted now.
createStreamAndWrite(t, cconn, 4, []byte("raboof"))
ctx, cancel = context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
sstr, err = sconn.AcceptStream(ctx)
require.NoError(t, err)
data, err = io.ReadAll(sstr)
require.NoError(t, err)
require.Equal(t, []byte("raboof"), data)
}
func TestServerReorderedUpgradeRequestTimeout(t *testing.T) {
timeout := scaleDuration(100 * time.Millisecond)
s := webtransport.Server{
H3: http3.Server{TLSConfig: tlsConf, EnableDatagrams: true},
ReorderingTimeout: timeout,
}
defer s.Close()
connChan := make(chan *webtransport.Session)
addHandler(t, &s, func(c *webtransport.Session) {
connChan <- c
})
udpConn, err := net.ListenUDP("udp", nil)
require.NoError(t, err)
port := udpConn.LocalAddr().(*net.UDPAddr).Port
go s.Serve(udpConn)
cconn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", port),
&tls.Config{RootCAs: certPool, NextProtos: []string{http3.NextProtoH3}},
&quic.Config{EnableDatagrams: true},
)
require.NoError(t, err)
// Open a new stream for a WebTransport session we'll establish later. Stream ID: 0.
str := createStreamAndWrite(t, cconn, 4, []byte("foobar"))
time.Sleep(2 * timeout)
tr := &http3.Transport{EnableDatagrams: true}
conn := tr.NewClientConn(cconn)
// Reordering was too long. The stream should now have been reset by the server.
_, err = str.Read([]byte{0})
var streamErr *quic.StreamError
require.ErrorAs(t, err, &streamErr)
require.Equal(t, webtransport.WebTransportBufferedStreamRejectedErrorCode, streamErr.ErrorCode)
// Now establish the session. Make sure we don't accept the stream.
requestStr, err := conn.OpenRequestStream(context.Background())
require.NoError(t, err)
require.NoError(t, requestStr.SendRequestHeader(newWebTransportRequest(t, fmt.Sprintf("https://localhost:%d/webtransport", port))))
rsp, err := requestStr.ReadResponse()
require.NoError(t, err)
require.Equal(t, http.StatusOK, rsp.StatusCode)
sconn := <-connChan
defer sconn.CloseWithError(0, "")
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
_, err = sconn.AcceptStream(ctx)
require.ErrorIs(t, err, context.DeadlineExceeded)
// Establish another stream and make sure it's accepted now.
createStreamAndWrite(t, cconn, 4, []byte("raboof"))
ctx, cancel = context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
sstr, err := sconn.AcceptStream(ctx)
require.NoError(t, err)
data, err := io.ReadAll(sstr)
require.NoError(t, err)
require.Equal(t, []byte("raboof"), data)
}
func TestServerReorderedMultipleStreams(t *testing.T) {
timeout := scaleDuration(150 * time.Millisecond)
s := webtransport.Server{
H3: http3.Server{TLSConfig: tlsConf, EnableDatagrams: true},
ReorderingTimeout: timeout,
}
defer s.Close()
connChan := make(chan *webtransport.Session)
addHandler(t, &s, func(c *webtransport.Session) {
connChan <- c
})
udpConn, err := net.ListenUDP("udp", nil)
require.NoError(t, err)
port := udpConn.LocalAddr().(*net.UDPAddr).Port
go s.Serve(udpConn)
cconn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", port),
&tls.Config{RootCAs: certPool, NextProtos: []string{http3.NextProtoH3}},
&quic.Config{EnableDatagrams: true},
)
require.NoError(t, err)
start := time.Now()
// Open a new stream for a WebTransport session we'll establish later. Stream ID: 0.
str1 := createStreamAndWrite(t, cconn, 8, []byte("foobar"))
// After a while, open another stream.
time.Sleep(timeout / 2)
// Open a new stream for a WebTransport session we'll establish later. Stream ID: 4.
createStreamAndWrite(t, cconn, 8, []byte("raboof"))
// Reordering was too long. The stream should now have been reset by the server.
_, err = str1.Read([]byte{0})
var streamErr *quic.StreamError
require.ErrorAs(t, err, &streamErr)
require.Equal(t, webtransport.WebTransportBufferedStreamRejectedErrorCode, streamErr.ErrorCode)
took := time.Since(start)
require.GreaterOrEqual(t, took, timeout)
require.Less(t, took, timeout*5/4)
tr := &http3.Transport{EnableDatagrams: true}
conn := tr.NewClientConn(cconn)
// Now establish the session. Make sure we don't accept the stream.
requestStr, err := conn.OpenRequestStream(context.Background())
require.NoError(t, err)
require.NoError(t, requestStr.SendRequestHeader(newWebTransportRequest(t, fmt.Sprintf("https://localhost:%d/webtransport", port))))
rsp, err := requestStr.ReadResponse()
require.NoError(t, err)
require.Equal(t, http.StatusOK, rsp.StatusCode)
sconn := <-connChan
defer sconn.CloseWithError(0, "")
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
sstr, err := sconn.AcceptStream(ctx)
require.NoError(t, err)
data, err := io.ReadAll(sstr)
require.NoError(t, err)
require.Equal(t, []byte("raboof"), data)
}
func TestServerSettingsCheck(t *testing.T) {
timeout := scaleDuration(150 * time.Millisecond)
s := webtransport.Server{
H3: http3.Server{TLSConfig: tlsConf, EnableDatagrams: true},
ReorderingTimeout: timeout,
}
errChan := make(chan error, 1)
mux := http.NewServeMux()
mux.HandleFunc("/webtransport", func(w http.ResponseWriter, r *http.Request) {
_, err := s.Upgrade(w, r)
w.WriteHeader(http.StatusNotImplemented)
errChan <- err
})
s.H3.Handler = mux
udpConn, err := net.ListenUDP("udp", nil)
require.NoError(t, err)
port := udpConn.LocalAddr().(*net.UDPAddr).Port
go s.Serve(udpConn)
cconn, err := quic.DialAddr(
context.Background(),
fmt.Sprintf("localhost:%d", port),
&tls.Config{RootCAs: certPool, NextProtos: []string{http3.NextProtoH3}},
&quic.Config{EnableDatagrams: true},
)
require.NoError(t, err)
tr := &http3.Transport{EnableDatagrams: false}
conn := tr.NewClientConn(cconn)
requestStr, err := conn.OpenRequestStream(context.Background())
require.NoError(t, err)
require.NoError(t, requestStr.SendRequestHeader(newWebTransportRequest(t, fmt.Sprintf("https://localhost:%d/webtransport", port))))
rsp, err := requestStr.ReadResponse()
require.NoError(t, err)
require.Equal(t, http.StatusNotImplemented, rsp.StatusCode)
require.ErrorContains(t, <-errChan, "webtransport: missing datagram support")
}
func TestImmediateClose(t *testing.T) {
s := webtransport.Server{H3: http3.Server{}}
require.NoError(t, s.Close())
}