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

     1  package wire
     2  
     3  import (
     4  	"github.com/metacubex/quic-go/internal/protocol"
     5  	"github.com/metacubex/quic-go/internal/qerr"
     6  	"github.com/metacubex/quic-go/quicvarint"
     7  
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("RESET_STREAM frame", func() {
    13  	Context("when parsing", func() {
    14  		It("accepts sample frame", func() {
    15  			data := encodeVarInt(0xdeadbeef)                  // stream ID
    16  			data = append(data, encodeVarInt(0x1337)...)      // error code
    17  			data = append(data, encodeVarInt(0x987654321)...) // byte offset
    18  			frame, l, err := parseResetStreamFrame(data, protocol.Version1)
    19  			Expect(err).ToNot(HaveOccurred())
    20  			Expect(frame.StreamID).To(Equal(protocol.StreamID(0xdeadbeef)))
    21  			Expect(frame.FinalSize).To(Equal(protocol.ByteCount(0x987654321)))
    22  			Expect(frame.ErrorCode).To(Equal(qerr.StreamErrorCode(0x1337)))
    23  			Expect(l).To(Equal(len(data)))
    24  		})
    25  
    26  		It("errors on EOFs", func() {
    27  			data := encodeVarInt(0xdeadbeef)                  // stream ID
    28  			data = append(data, encodeVarInt(0x1337)...)      // error code
    29  			data = append(data, encodeVarInt(0x987654321)...) // byte offset
    30  			_, l, err := parseResetStreamFrame(data, protocol.Version1)
    31  			Expect(err).NotTo(HaveOccurred())
    32  			Expect(l).To(Equal(len(data)))
    33  			for i := range data {
    34  				_, _, err := parseResetStreamFrame(data[:i], protocol.Version1)
    35  				Expect(err).To(HaveOccurred())
    36  			}
    37  		})
    38  	})
    39  
    40  	Context("when writing", func() {
    41  		It("writes a sample frame", func() {
    42  			frame := ResetStreamFrame{
    43  				StreamID:  0x1337,
    44  				FinalSize: 0x11223344decafbad,
    45  				ErrorCode: 0xcafe,
    46  			}
    47  			b, err := frame.Append(nil, protocol.Version1)
    48  			Expect(err).ToNot(HaveOccurred())
    49  			expected := []byte{resetStreamFrameType}
    50  			expected = append(expected, encodeVarInt(0x1337)...)
    51  			expected = append(expected, encodeVarInt(0xcafe)...)
    52  			expected = append(expected, encodeVarInt(0x11223344decafbad)...)
    53  			Expect(b).To(Equal(expected))
    54  		})
    55  
    56  		It("has the correct length", func() {
    57  			rst := ResetStreamFrame{
    58  				StreamID:  0x1337,
    59  				FinalSize: 0x1234567,
    60  				ErrorCode: 0xde,
    61  			}
    62  			expectedLen := 1 + quicvarint.Len(0x1337) + quicvarint.Len(0x1234567) + 2
    63  			Expect(rst.Length(protocol.Version1)).To(BeEquivalentTo(expectedLen))
    64  		})
    65  	})
    66  })