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
|
package wire
import (
"fmt"
"io"
"testing"
"github.com/quic-go/quic-go/internal/protocol"
"github.com/quic-go/quic-go/quicvarint"
"github.com/stretchr/testify/require"
)
func TestParseMaxStreamsFrameBidirectional(t *testing.T) {
data := encodeVarInt(0xdecaf)
f, l, err := parseMaxStreamsFrame(data, FrameTypeBidiMaxStreams, protocol.Version1)
require.NoError(t, err)
require.Equal(t, protocol.StreamTypeBidi, f.Type)
require.EqualValues(t, 0xdecaf, f.MaxStreamNum)
require.Equal(t, len(data), l)
}
func TestParseMaxStreamsFrameUnidirectional(t *testing.T) {
data := encodeVarInt(0xdecaf)
f, l, err := parseMaxStreamsFrame(data, FrameTypeUniMaxStreams, protocol.Version1)
require.NoError(t, err)
require.Equal(t, protocol.StreamTypeUni, f.Type)
require.EqualValues(t, 0xdecaf, f.MaxStreamNum)
require.Equal(t, len(data), l)
}
func TestParseMaxStreamsErrorsOnEOF(t *testing.T) {
const typ = 0x1d
data := encodeVarInt(0xdeadbeefcafe13)
_, l, err := parseMaxStreamsFrame(data, typ, protocol.Version1)
require.NoError(t, err)
require.Equal(t, len(data), l)
for i := range data {
_, _, err := parseMaxStreamsFrame(data[:i], typ, protocol.Version1)
require.Equal(t, io.EOF, err)
}
}
func TestParseMaxStreamsMaxValue(t *testing.T) {
for _, streamType := range []protocol.StreamType{protocol.StreamTypeUni, protocol.StreamTypeBidi} {
var streamTypeStr string
if streamType == protocol.StreamTypeUni {
streamTypeStr = "unidirectional"
} else {
streamTypeStr = "bidirectional"
}
t.Run(streamTypeStr, func(t *testing.T) {
f := &MaxStreamsFrame{
Type: streamType,
MaxStreamNum: protocol.MaxStreamCount,
}
b, err := f.Append(nil, protocol.Version1)
require.NoError(t, err)
typ, l, err := quicvarint.Parse(b)
require.NoError(t, err)
b = b[l:]
frame, _, err := parseMaxStreamsFrame(b, FrameType(typ), protocol.Version1)
require.NoError(t, err)
require.Equal(t, f, frame)
})
}
}
func TestParseMaxStreamsErrorsOnTooLargeStreamCount(t *testing.T) {
for _, streamType := range []protocol.StreamType{protocol.StreamTypeUni, protocol.StreamTypeBidi} {
var streamTypeStr string
if streamType == protocol.StreamTypeUni {
streamTypeStr = "unidirectional"
} else {
streamTypeStr = "bidirectional"
}
t.Run(streamTypeStr, func(t *testing.T) {
f := &MaxStreamsFrame{
Type: streamType,
MaxStreamNum: protocol.MaxStreamCount + 1,
}
b, err := f.Append(nil, protocol.Version1)
require.NoError(t, err)
typ, l, err := quicvarint.Parse(b)
require.NoError(t, err)
b = b[l:]
_, _, err = parseMaxStreamsFrame(b, FrameType(typ), protocol.Version1)
require.EqualError(t, err, fmt.Sprintf("%d exceeds the maximum stream count", protocol.MaxStreamCount+1))
})
}
}
func TestWriteMaxStreamsBidirectional(t *testing.T) {
f := &MaxStreamsFrame{
Type: protocol.StreamTypeBidi,
MaxStreamNum: 0xdeadbeef,
}
b, err := f.Append(nil, protocol.Version1)
require.NoError(t, err)
expected := []byte{byte(FrameTypeBidiMaxStreams)}
expected = append(expected, encodeVarInt(0xdeadbeef)...)
require.Equal(t, expected, b)
require.Len(t, b, int(f.Length(protocol.Version1)))
}
func TestWriteMaxStreamsUnidirectional(t *testing.T) {
f := &MaxStreamsFrame{
Type: protocol.StreamTypeUni,
MaxStreamNum: 0xdecafbad,
}
b, err := f.Append(nil, protocol.Version1)
require.NoError(t, err)
expected := []byte{byte(FrameTypeUniMaxStreams)}
expected = append(expected, encodeVarInt(0xdecafbad)...)
require.Equal(t, expected, b)
require.Len(t, b, int(f.Length(protocol.Version1)))
}
|