github.com/tumi8/quic-go@v0.37.4-tum/noninternal/wire/streams_blocked_frame_test.go (about)

     1  package wire
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  
     8  	"github.com/tumi8/quic-go/noninternal/protocol"
     9  	"github.com/tumi8/quic-go/quicvarint"
    10  
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("STREAMS_BLOCKED frame", func() {
    16  	Context("parsing", func() {
    17  		It("accepts a frame for bidirectional streams", func() {
    18  			expected := encodeVarInt(0x1337)
    19  			b := bytes.NewReader(expected)
    20  			f, err := parseStreamsBlockedFrame(b, bidiStreamBlockedFrameType, protocol.Version1)
    21  			Expect(err).ToNot(HaveOccurred())
    22  			Expect(f.Type).To(Equal(protocol.StreamTypeBidi))
    23  			Expect(f.StreamLimit).To(BeEquivalentTo(0x1337))
    24  			Expect(b.Len()).To(BeZero())
    25  		})
    26  
    27  		It("accepts a frame for unidirectional streams", func() {
    28  			expected := encodeVarInt(0x7331)
    29  			b := bytes.NewReader(expected)
    30  			f, err := parseStreamsBlockedFrame(b, uniStreamBlockedFrameType, protocol.Version1)
    31  			Expect(err).ToNot(HaveOccurred())
    32  			Expect(f.Type).To(Equal(protocol.StreamTypeUni))
    33  			Expect(f.StreamLimit).To(BeEquivalentTo(0x7331))
    34  			Expect(b.Len()).To(BeZero())
    35  		})
    36  
    37  		It("errors on EOFs", func() {
    38  			data := encodeVarInt(0x12345678)
    39  			b := bytes.NewReader(data)
    40  			_, err := parseStreamsBlockedFrame(b, bidiStreamBlockedFrameType, protocol.Version1)
    41  			Expect(err).ToNot(HaveOccurred())
    42  			for i := range data {
    43  				_, err := parseStreamsBlockedFrame(bytes.NewReader(data[:i]), bidiStreamBlockedFrameType, 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 := &StreamsBlockedFrame{
    53  					Type:        streamType,
    54  					StreamLimit: 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 := parseStreamsBlockedFrame(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 := &StreamsBlockedFrame{
    68  					Type:        streamType,
    69  					StreamLimit: 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 = parseStreamsBlockedFrame(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("writes a frame for bidirectional streams", func() {
    84  			f := StreamsBlockedFrame{
    85  				Type:        protocol.StreamTypeBidi,
    86  				StreamLimit: 0xdeadbeefcafe,
    87  			}
    88  			b, err := f.Append(nil, protocol.Version1)
    89  			Expect(err).ToNot(HaveOccurred())
    90  			expected := []byte{bidiStreamBlockedFrameType}
    91  			expected = append(expected, encodeVarInt(0xdeadbeefcafe)...)
    92  			Expect(b).To(Equal(expected))
    93  		})
    94  
    95  		It("writes a frame for unidirectional streams", func() {
    96  			f := StreamsBlockedFrame{
    97  				Type:        protocol.StreamTypeUni,
    98  				StreamLimit: 0xdeadbeefcafe,
    99  			}
   100  			b, err := f.Append(nil, protocol.Version1)
   101  			Expect(err).ToNot(HaveOccurred())
   102  			expected := []byte{uniStreamBlockedFrameType}
   103  			expected = append(expected, encodeVarInt(0xdeadbeefcafe)...)
   104  			Expect(b).To(Equal(expected))
   105  		})
   106  
   107  		It("has the correct min length", func() {
   108  			frame := StreamsBlockedFrame{StreamLimit: 0x123456}
   109  			Expect(frame.Length(0)).To(Equal(protocol.ByteCount(1) + quicvarint.Len(0x123456)))
   110  		})
   111  	})
   112  })