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