github.com/quic-go/quic-go@v0.44.0/internal/wire/streams_blocked_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("STREAMS_BLOCKED frame", func() {
    15  	Context("parsing", func() {
    16  		It("accepts a frame for bidirectional streams", func() {
    17  			data := encodeVarInt(0x1337)
    18  			f, l, err := parseStreamsBlockedFrame(data, bidiStreamBlockedFrameType, protocol.Version1)
    19  			Expect(err).ToNot(HaveOccurred())
    20  			Expect(f.Type).To(Equal(protocol.StreamTypeBidi))
    21  			Expect(f.StreamLimit).To(BeEquivalentTo(0x1337))
    22  			Expect(l).To(Equal(len(data)))
    23  		})
    24  
    25  		It("accepts a frame for unidirectional streams", func() {
    26  			data := encodeVarInt(0x7331)
    27  			f, l, err := parseStreamsBlockedFrame(data, uniStreamBlockedFrameType, protocol.Version1)
    28  			Expect(err).ToNot(HaveOccurred())
    29  			Expect(f.Type).To(Equal(protocol.StreamTypeUni))
    30  			Expect(f.StreamLimit).To(BeEquivalentTo(0x7331))
    31  			Expect(l).To(Equal(len(data)))
    32  		})
    33  
    34  		It("errors on EOFs", func() {
    35  			data := encodeVarInt(0x12345678)
    36  			_, l, err := parseStreamsBlockedFrame(data, bidiStreamBlockedFrameType, protocol.Version1)
    37  			Expect(err).ToNot(HaveOccurred())
    38  			Expect(l).To(Equal(len(data)))
    39  			for i := range data {
    40  				_, _, err := parseStreamsBlockedFrame(data[:i], bidiStreamBlockedFrameType, protocol.Version1)
    41  				Expect(err).To(MatchError(io.EOF))
    42  			}
    43  		})
    44  
    45  		for _, t := range []protocol.StreamType{protocol.StreamTypeUni, protocol.StreamTypeBidi} {
    46  			streamType := t
    47  
    48  			It("accepts a frame containing the maximum stream count", func() {
    49  				f := &StreamsBlockedFrame{
    50  					Type:        streamType,
    51  					StreamLimit: protocol.MaxStreamCount,
    52  				}
    53  				b, err := f.Append(nil, protocol.Version1)
    54  				Expect(err).ToNot(HaveOccurred())
    55  				typ, l, err := quicvarint.Parse(b)
    56  				Expect(err).ToNot(HaveOccurred())
    57  				b = b[l:]
    58  				frame, l, err := parseStreamsBlockedFrame(b, typ, protocol.Version1)
    59  				Expect(err).ToNot(HaveOccurred())
    60  				Expect(frame).To(Equal(f))
    61  				Expect(l).To(Equal(len(b)))
    62  			})
    63  
    64  			It("errors when receiving a too large stream count", func() {
    65  				f := &StreamsBlockedFrame{
    66  					Type:        streamType,
    67  					StreamLimit: 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 = parseStreamsBlockedFrame(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("writes a frame for bidirectional streams", func() {
    82  			f := StreamsBlockedFrame{
    83  				Type:        protocol.StreamTypeBidi,
    84  				StreamLimit: 0xdeadbeefcafe,
    85  			}
    86  			b, err := f.Append(nil, protocol.Version1)
    87  			Expect(err).ToNot(HaveOccurred())
    88  			expected := []byte{bidiStreamBlockedFrameType}
    89  			expected = append(expected, encodeVarInt(0xdeadbeefcafe)...)
    90  			Expect(b).To(Equal(expected))
    91  		})
    92  
    93  		It("writes a frame for unidirectional streams", func() {
    94  			f := StreamsBlockedFrame{
    95  				Type:        protocol.StreamTypeUni,
    96  				StreamLimit: 0xdeadbeefcafe,
    97  			}
    98  			b, err := f.Append(nil, protocol.Version1)
    99  			Expect(err).ToNot(HaveOccurred())
   100  			expected := []byte{uniStreamBlockedFrameType}
   101  			expected = append(expected, encodeVarInt(0xdeadbeefcafe)...)
   102  			Expect(b).To(Equal(expected))
   103  		})
   104  
   105  		It("has the correct min length", func() {
   106  			frame := StreamsBlockedFrame{StreamLimit: 0x123456}
   107  			Expect(frame.Length(0)).To(Equal(1 + protocol.ByteCount(quicvarint.Len(0x123456))))
   108  		})
   109  	})
   110  })