github.com/quic-go/quic-go@v0.44.0/internal/wire/max_streams_frame_test.go (about)

     1  package wire
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/quic-go/quic-go/internal/protocol"
     8  	"github.com/quic-go/quic-go/quicvarint"
     9  
    10  	. "github.com/onsi/ginkgo/v2"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("MAX_STREAMS frame", func() {
    15  	Context("parsing", func() {
    16  		It("accepts a frame for a bidirectional stream", func() {
    17  			data := encodeVarInt(0xdecaf)
    18  			f, l, err := parseMaxStreamsFrame(data, bidiMaxStreamsFrameType, protocol.Version1)
    19  			Expect(err).ToNot(HaveOccurred())
    20  			Expect(f.Type).To(Equal(protocol.StreamTypeBidi))
    21  			Expect(f.MaxStreamNum).To(BeEquivalentTo(0xdecaf))
    22  			Expect(l).To(Equal(len(data)))
    23  		})
    24  
    25  		It("accepts a frame for a bidirectional stream", func() {
    26  			data := encodeVarInt(0xdecaf)
    27  			f, l, err := parseMaxStreamsFrame(data, uniMaxStreamsFrameType, protocol.Version1)
    28  			Expect(err).ToNot(HaveOccurred())
    29  			Expect(f.Type).To(Equal(protocol.StreamTypeUni))
    30  			Expect(f.MaxStreamNum).To(BeEquivalentTo(0xdecaf))
    31  			Expect(l).To(Equal(len(data)))
    32  		})
    33  
    34  		It("errors on EOFs", func() {
    35  			const typ = 0x1d
    36  			data := encodeVarInt(0xdeadbeefcafe13)
    37  			_, l, err := parseMaxStreamsFrame(data, typ, protocol.Version1)
    38  			Expect(err).NotTo(HaveOccurred())
    39  			Expect(l).To(Equal(len(data)))
    40  			for i := range data {
    41  				_, _, err := parseMaxStreamsFrame(data[:i], typ, protocol.Version1)
    42  				Expect(err).To(MatchError(io.EOF))
    43  			}
    44  		})
    45  
    46  		for _, t := range []protocol.StreamType{protocol.StreamTypeUni, protocol.StreamTypeBidi} {
    47  			streamType := t
    48  
    49  			It("accepts a frame containing the maximum stream count", func() {
    50  				f := &MaxStreamsFrame{
    51  					Type:         streamType,
    52  					MaxStreamNum: protocol.MaxStreamCount,
    53  				}
    54  				b, err := f.Append(nil, protocol.Version1)
    55  				Expect(err).ToNot(HaveOccurred())
    56  				typ, l, err := quicvarint.Parse(b)
    57  				Expect(err).ToNot(HaveOccurred())
    58  				b = b[l:]
    59  				frame, _, err := parseMaxStreamsFrame(b, typ, protocol.Version1)
    60  				Expect(err).ToNot(HaveOccurred())
    61  				Expect(frame).To(Equal(f))
    62  			})
    63  
    64  			It("errors when receiving a too large stream count", func() {
    65  				f := &MaxStreamsFrame{
    66  					Type:         streamType,
    67  					MaxStreamNum: protocol.MaxStreamCount + 1,
    68  				}
    69  				b, err := f.Append(nil, protocol.Version1)
    70  				Expect(err).ToNot(HaveOccurred())
    71  				typ, l, err := quicvarint.Parse(b)
    72  				Expect(err).ToNot(HaveOccurred())
    73  				b = b[l:]
    74  				_, _, err = parseMaxStreamsFrame(b, typ, protocol.Version1)
    75  				Expect(err).To(MatchError(fmt.Sprintf("%d exceeds the maximum stream count", protocol.MaxStreamCount+1)))
    76  			})
    77  		}
    78  	})
    79  
    80  	Context("writing", func() {
    81  		It("for a bidirectional stream", func() {
    82  			f := &MaxStreamsFrame{
    83  				Type:         protocol.StreamTypeBidi,
    84  				MaxStreamNum: 0xdeadbeef,
    85  			}
    86  			b, err := f.Append(nil, protocol.Version1)
    87  			Expect(err).ToNot(HaveOccurred())
    88  			expected := []byte{bidiMaxStreamsFrameType}
    89  			expected = append(expected, encodeVarInt(0xdeadbeef)...)
    90  			Expect(b).To(Equal(expected))
    91  		})
    92  
    93  		It("for a unidirectional stream", func() {
    94  			f := &MaxStreamsFrame{
    95  				Type:         protocol.StreamTypeUni,
    96  				MaxStreamNum: 0xdecafbad,
    97  			}
    98  			b, err := f.Append(nil, protocol.Version1)
    99  			Expect(err).ToNot(HaveOccurred())
   100  			expected := []byte{uniMaxStreamsFrameType}
   101  			expected = append(expected, encodeVarInt(0xdecafbad)...)
   102  			Expect(b).To(Equal(expected))
   103  		})
   104  
   105  		It("has the correct length", func() {
   106  			frame := MaxStreamsFrame{MaxStreamNum: 0x1337}
   107  			Expect(frame.Length(protocol.Version1)).To(BeEquivalentTo(1 + quicvarint.Len(0x1337)))
   108  		})
   109  	})
   110  })