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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
package http3
import (
"bytes"
"context"
"io"
"testing"
"time"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/quicvarint"
"github.com/stretchr/testify/require"
)
func TestConnReceiveSettings(t *testing.T) {
clientConn, serverConn := newConnPair(t)
conn := newConnection(
serverConn.Context(),
serverConn,
false,
true, // server
nil,
0,
)
b := quicvarint.Append(nil, streamTypeControlStream)
b = (&settingsFrame{
Datagram: true,
ExtendedConnect: true,
Other: map[uint64]uint64{1337: 42},
}).Append(b)
controlStr, err := clientConn.OpenUniStream()
require.NoError(t, err)
_, err = controlStr.Write(b)
require.NoError(t, err)
done := make(chan struct{})
go func() {
defer close(done)
conn.handleUnidirectionalStreams(nil)
}()
select {
case <-conn.ReceivedSettings():
case <-time.After(time.Second):
t.Fatal("timeout waiting for settings")
}
settings := conn.Settings()
require.True(t, settings.EnableDatagrams)
require.True(t, settings.EnableExtendedConnect)
require.Equal(t, map[uint64]uint64{1337: 42}, settings.Other)
}
func TestConnRejectDuplicateStreams(t *testing.T) {
t.Run("control stream", func(t *testing.T) {
testConnRejectDuplicateStreams(t, streamTypeControlStream)
})
t.Run("encoder stream", func(t *testing.T) {
testConnRejectDuplicateStreams(t, streamTypeQPACKEncoderStream)
})
t.Run("decoder stream", func(t *testing.T) {
testConnRejectDuplicateStreams(t, streamTypeQPACKDecoderStream)
})
}
func testConnRejectDuplicateStreams(t *testing.T, typ uint64) {
clientConn, serverConn := newConnPair(t)
conn := newConnection(
context.Background(),
serverConn,
false,
true, // server
nil,
0,
)
b := quicvarint.Append(nil, typ)
if typ == streamTypeControlStream {
b = (&settingsFrame{}).Append(b)
}
controlStr1, err := clientConn.OpenUniStream()
require.NoError(t, err)
_, err = controlStr1.Write(b)
require.NoError(t, err)
controlStr2, err := clientConn.OpenUniStream()
require.NoError(t, err)
_, err = controlStr2.Write(b)
require.NoError(t, err)
done := make(chan struct{})
go func() {
defer close(done)
conn.handleUnidirectionalStreams(nil)
}()
select {
case <-clientConn.Context().Done():
require.ErrorIs(t,
context.Cause(clientConn.Context()),
&quic.ApplicationError{Remote: true, ErrorCode: quic.ApplicationErrorCode(ErrCodeStreamCreationError)},
)
case <-time.After(time.Second):
t.Fatal("timeout waiting for duplicate stream")
}
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("timeout")
}
}
func TestConnResetUnknownUniStream(t *testing.T) {
clientConn, serverConn := newConnPair(t)
conn := newConnection(
context.Background(),
serverConn,
false,
true, // server
nil,
0,
)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x1337))
str, err := clientConn.OpenUniStream()
require.NoError(t, err)
_, err = str.Write(buf.Bytes())
require.NoError(t, err)
go conn.handleUnidirectionalStreams(nil)
expectStreamWriteReset(t, str, quic.StreamErrorCode(ErrCodeStreamCreationError))
}
func TestConnControlStreamFailures(t *testing.T) {
t.Run("missing SETTINGS", func(t *testing.T) {
testConnControlStreamFailures(t, (&dataFrame{}).Append(nil), nil, ErrCodeMissingSettings)
})
t.Run("frame error", func(t *testing.T) {
testConnControlStreamFailures(t,
// 1337 is invalid value for the Extended CONNECT setting
(&settingsFrame{Other: map[uint64]uint64{settingExtendedConnect: 1337}}).Append(nil),
nil,
ErrCodeFrameError,
)
})
t.Run("control stream closed before SETTINGS", func(t *testing.T) {
testConnControlStreamFailures(t, nil, io.EOF, ErrCodeClosedCriticalStream)
})
t.Run("control stream reset before SETTINGS", func(t *testing.T) {
testConnControlStreamFailures(t,
nil,
&quic.StreamError{Remote: true, ErrorCode: 42},
ErrCodeClosedCriticalStream,
)
})
}
func TestConnGoAwayFailures(t *testing.T) {
t.Run("invalid frame", func(t *testing.T) {
b := (&settingsFrame{}).Append(nil)
// 1337 is invalid value for the Extended CONNECT setting
b = (&settingsFrame{Other: map[uint64]uint64{settingExtendedConnect: 1337}}).Append(b)
testConnControlStreamFailures(t, b, nil, ErrCodeFrameError)
})
t.Run("not a GOAWAY", func(t *testing.T) {
b := (&settingsFrame{}).Append(nil)
// GOAWAY is the only allowed frame type after SETTINGS
b = (&headersFrame{}).Append(b)
testConnControlStreamFailures(t, b, nil, ErrCodeFrameUnexpected)
})
t.Run("stream closed before GOAWAY", func(t *testing.T) {
testConnControlStreamFailures(t, (&settingsFrame{}).Append(nil), io.EOF, ErrCodeClosedCriticalStream)
})
t.Run("stream reset before GOAWAY", func(t *testing.T) {
testConnControlStreamFailures(t,
(&settingsFrame{}).Append(nil),
&quic.StreamError{Remote: true, ErrorCode: 42},
ErrCodeClosedCriticalStream,
)
})
t.Run("invalid stream ID", func(t *testing.T) {
data := (&settingsFrame{}).Append(nil)
data = (&goAwayFrame{StreamID: 1}).Append(data)
testConnControlStreamFailures(t, data, nil, ErrCodeIDError)
})
t.Run("increased stream ID", func(t *testing.T) {
data := (&settingsFrame{}).Append(nil)
data = (&goAwayFrame{StreamID: 4}).Append(data)
data = (&goAwayFrame{StreamID: 8}).Append(data)
testConnControlStreamFailures(t, data, nil, ErrCodeIDError)
})
}
func testConnControlStreamFailures(t *testing.T, data []byte, readErr error, expectedErr ErrCode) {
clientConn, serverConn := newConnPair(t)
conn := newConnection(
clientConn.Context(),
clientConn,
false,
false, // client
nil,
0,
)
controlStr, err := serverConn.OpenUniStream()
require.NoError(t, err)
_, err = controlStr.Write(quicvarint.Append(nil, streamTypeControlStream))
require.NoError(t, err)
done := make(chan struct{})
go func() {
defer close(done)
conn.handleUnidirectionalStreams(nil)
}()
conn.openRequestStream(context.Background(), nil, nil, true, 1000)
switch readErr {
case nil:
_, err = controlStr.Write(data)
require.NoError(t, err)
case io.EOF:
_, err = controlStr.Write(data)
require.NoError(t, err)
require.NoError(t, controlStr.Close())
default:
// make sure the stream type is received
time.Sleep(scaleDuration(10 * time.Millisecond))
controlStr.CancelWrite(1337)
}
select {
case <-serverConn.Context().Done():
require.ErrorIs(t,
context.Cause(serverConn.Context()),
&quic.ApplicationError{Remote: true, ErrorCode: quic.ApplicationErrorCode(expectedErr)},
)
case <-time.After(time.Second):
t.Fatal("timeout waiting for close")
}
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("timeout")
}
}
func TestConnGoAway(t *testing.T) {
t.Run("no active streams", func(t *testing.T) {
testConnGoAway(t, false)
})
t.Run("active stream", func(t *testing.T) {
testConnGoAway(t, true)
})
}
func testConnGoAway(t *testing.T, withStream bool) {
clientConn, serverConn := newConnPair(t)
conn := newConnection(
clientConn.Context(),
clientConn,
false,
false, // client
nil,
0,
)
b := quicvarint.Append(nil, streamTypeControlStream)
b = (&settingsFrame{}).Append(b)
b = (&goAwayFrame{StreamID: 8}).Append(b)
var str *RequestStream
if withStream {
s, err := conn.openRequestStream(context.Background(), nil, nil, true, 1000)
require.NoError(t, err)
str = s
}
controlStr, err := serverConn.OpenUniStream()
require.NoError(t, err)
_, err = controlStr.Write(b)
require.NoError(t, err)
go conn.handleUnidirectionalStreams(nil)
// the connection should be closed after the stream is closed
if withStream {
select {
case <-serverConn.Context().Done():
t.Fatal("connection closed")
case <-time.After(scaleDuration(10 * time.Millisecond)):
}
// The stream ID in the GOAWAY frame is 8, so it's possible to open stream 4.
str2, err := conn.openRequestStream(context.Background(), nil, nil, true, 1000)
require.NoError(t, err)
str2.Close()
str2.CancelRead(1337)
// It's not possible to open stream 8.
_, err = conn.openRequestStream(context.Background(), nil, nil, true, 1000)
require.ErrorIs(t, err, errGoAway)
str.Close()
str.CancelRead(1337)
}
select {
case <-serverConn.Context().Done():
require.ErrorIs(t,
context.Cause(serverConn.Context()),
&quic.ApplicationError{Remote: true, ErrorCode: quic.ApplicationErrorCode(ErrCodeNoError)},
)
case <-time.After(time.Second):
t.Fatal("timeout waiting for close")
}
}
func TestConnRejectPushStream(t *testing.T) {
t.Run("client", func(t *testing.T) {
testConnRejectPushStream(t, false, ErrCodeStreamCreationError)
})
t.Run("server", func(t *testing.T) {
testConnRejectPushStream(t, true, ErrCodeIDError)
})
}
func testConnRejectPushStream(t *testing.T, isServer bool, expectedErr ErrCode) {
clientConn, serverConn := newConnPair(t)
conn := newConnection(
clientConn.Context(),
clientConn,
false,
!isServer,
nil,
0,
)
buf := bytes.NewBuffer(quicvarint.Append(nil, streamTypePushStream))
controlStr, err := serverConn.OpenUniStream()
require.NoError(t, err)
_, err = controlStr.Write(buf.Bytes())
require.NoError(t, err)
done := make(chan struct{})
go func() {
defer close(done)
conn.handleUnidirectionalStreams(nil)
}()
select {
case <-serverConn.Context().Done():
require.ErrorIs(t,
context.Cause(serverConn.Context()),
&quic.ApplicationError{Remote: true, ErrorCode: quic.ApplicationErrorCode(expectedErr)},
)
case <-time.After(time.Second):
t.Fatal("timeout waiting for close")
}
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("timeout")
}
}
func TestConnInconsistentDatagramSupport(t *testing.T) {
clientConn, serverConn := newConnPair(t)
conn := newConnection(
clientConn.Context(),
clientConn,
true,
false, // client
nil,
0,
)
b := quicvarint.Append(nil, streamTypeControlStream)
b = (&settingsFrame{Datagram: true}).Append(b)
controlStr, err := serverConn.OpenUniStream()
require.NoError(t, err)
_, err = controlStr.Write(b)
require.NoError(t, err)
go conn.handleUnidirectionalStreams(nil)
select {
case <-serverConn.Context().Done():
err := context.Cause(serverConn.Context())
require.ErrorIs(t, err, &quic.ApplicationError{Remote: true, ErrorCode: quic.ApplicationErrorCode(ErrCodeSettingsError)})
require.ErrorContains(t, err, "missing QUIC Datagram support")
case <-time.After(time.Second):
t.Fatal("timeout waiting for close")
}
}
func TestConnSendAndReceiveDatagram(t *testing.T) {
clientConn, serverConn := newConnPairWithDatagrams(t)
conn := newConnection(
clientConn.Context(),
clientConn,
true,
false, // client
nil,
0,
)
b := quicvarint.Append(nil, streamTypeControlStream)
b = (&settingsFrame{Datagram: true}).Append(b)
controlStr, err := serverConn.OpenUniStream()
require.NoError(t, err)
_, err = controlStr.Write(b)
require.NoError(t, err)
go conn.handleUnidirectionalStreams(nil)
const strID = 4
// first deliver a datagram...
// since the stream is not open yet, it will be dropped
quarterStreamID := quicvarint.Append([]byte{}, strID/4)
require.NoError(t, serverConn.SendDatagram(append(quarterStreamID, []byte("foo")...)))
time.Sleep(scaleDuration(10 * time.Millisecond)) // give the datagram a chance to be delivered
// don't use stream 0, since that makes it hard to test that the quarter stream ID is used
str1, err := conn.openRequestStream(context.Background(), nil, nil, true, 1000)
require.NoError(t, err)
str1.Close()
str, err := conn.openRequestStream(context.Background(), nil, nil, true, 1000)
require.NoError(t, err)
require.Equal(t, quic.StreamID(strID), str.StreamID())
// now open the stream...
require.NoError(t, serverConn.SendDatagram(append(quarterStreamID, []byte("bar")...)))
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
data, err := str.ReceiveDatagram(ctx)
require.NoError(t, err)
require.Equal(t, []byte("bar"), data)
// now send a datagram
str.SendDatagram([]byte("foobaz"))
expected := quicvarint.Append([]byte{}, strID/4)
expected = append(expected, []byte("foobaz")...)
data, err = serverConn.ReceiveDatagram(ctx)
require.NoError(t, err)
require.Equal(t, expected, data)
}
func TestConnDatagramFailures(t *testing.T) {
t.Run("invalid varint", func(t *testing.T) {
testConnDatagramFailures(t, []byte{128})
})
t.Run("invalid quarter stream ID", func(t *testing.T) {
testConnDatagramFailures(t, quicvarint.Append([]byte{}, maxQuarterStreamID+1))
})
}
func testConnDatagramFailures(t *testing.T, datagram []byte) {
clientConn, serverConn := newConnPairWithDatagrams(t)
conn := newConnection(
clientConn.Context(),
clientConn,
true,
false, // client
nil,
0,
)
b := quicvarint.Append(nil, streamTypeControlStream)
b = (&settingsFrame{Datagram: true}).Append(b)
controlStr, err := serverConn.OpenUniStream()
require.NoError(t, err)
_, err = controlStr.Write(b)
require.NoError(t, err)
require.NoError(t, serverConn.SendDatagram(datagram))
go func() { conn.handleUnidirectionalStreams(nil) }()
select {
case <-serverConn.Context().Done():
require.ErrorIs(t,
context.Cause(serverConn.Context()),
&quic.ApplicationError{Remote: true, ErrorCode: quic.ApplicationErrorCode(ErrCodeDatagramError)},
)
case <-time.After(time.Second):
t.Fatal("timeout waiting for close")
}
}
|