github.com/metacubex/quic-go@v0.44.1-0.20240520163451-20b689a59136/internal/wire/max_data_frame_test.go (about)

     1  package wire
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/metacubex/quic-go/internal/protocol"
     7  	"github.com/metacubex/quic-go/quicvarint"
     8  
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("MAX_DATA frame", func() {
    14  	Context("when parsing", func() {
    15  		It("accepts sample frame", func() {
    16  			data := encodeVarInt(0xdecafbad123456) // byte offset
    17  			frame, l, err := parseMaxDataFrame(data, protocol.Version1)
    18  			Expect(err).ToNot(HaveOccurred())
    19  			Expect(frame.MaximumData).To(Equal(protocol.ByteCount(0xdecafbad123456)))
    20  			Expect(l).To(Equal(len(data)))
    21  		})
    22  
    23  		It("errors on EOFs", func() {
    24  			data := encodeVarInt(0xdecafbad1234567) // byte offset
    25  			_, l, err := parseMaxDataFrame(data, protocol.Version1)
    26  			Expect(err).NotTo(HaveOccurred())
    27  			Expect(l).To(Equal(len(data)))
    28  			for i := range data {
    29  				_, _, err := parseMaxDataFrame(data[:i], protocol.Version1)
    30  				Expect(err).To(MatchError(io.EOF))
    31  			}
    32  		})
    33  	})
    34  
    35  	Context("writing", func() {
    36  		It("has proper length", func() {
    37  			f := &MaxDataFrame{
    38  				MaximumData: 0xdeadbeef,
    39  			}
    40  			Expect(f.Length(protocol.Version1)).To(BeEquivalentTo(1 + quicvarint.Len(0xdeadbeef)))
    41  		})
    42  
    43  		It("writes a MAX_DATA frame", func() {
    44  			f := &MaxDataFrame{
    45  				MaximumData: 0xdeadbeefcafe,
    46  			}
    47  			b, err := f.Append(nil, protocol.Version1)
    48  			Expect(err).ToNot(HaveOccurred())
    49  			expected := []byte{maxDataFrameType}
    50  			expected = append(expected, encodeVarInt(0xdeadbeefcafe)...)
    51  			Expect(b).To(Equal(expected))
    52  		})
    53  	})
    54  })