github.com/daeuniverse/quic-go@v0.0.0-20240413031024-943f218e0810/internal/wire/max_streams_frame_test.go (about)

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