From 5afe35328a58952b6aaa7294e8f9974d9d4ff1b9 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Fri, 3 Nov 2017 10:58:35 +0700 Subject: [PATCH] implement parsing and writing of MAX_STREAM_ID frames --- internal/wire/max_stream_id_frame.go | 37 ++++++++++++++++ internal/wire/max_stream_id_frame_test.go | 51 +++++++++++++++++++++++ packet_unpacker.go | 6 ++- packet_unpacker_test.go | 12 ++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 internal/wire/max_stream_id_frame.go create mode 100644 internal/wire/max_stream_id_frame_test.go diff --git a/internal/wire/max_stream_id_frame.go b/internal/wire/max_stream_id_frame.go new file mode 100644 index 00000000..6d1aeae7 --- /dev/null +++ b/internal/wire/max_stream_id_frame.go @@ -0,0 +1,37 @@ +package wire + +import ( + "bytes" + + "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/utils" +) + +// A MaxStreamIDFrame is a MAX_STREAM_ID frame +type MaxStreamIDFrame struct { + StreamID protocol.StreamID +} + +// ParseMaxStreamIDFrame parses a MAX_STREAM_ID frame +func ParseMaxStreamIDFrame(r *bytes.Reader, _ protocol.VersionNumber) (*MaxStreamIDFrame, error) { + // read the Type byte + if _, err := r.ReadByte(); err != nil { + return nil, err + } + streamID, err := utils.ReadVarInt(r) + if err != nil { + return nil, err + } + return &MaxStreamIDFrame{StreamID: protocol.StreamID(streamID)}, nil +} + +func (f *MaxStreamIDFrame) Write(b *bytes.Buffer, _ protocol.VersionNumber) error { + b.WriteByte(0x6) + utils.WriteVarInt(b, uint64(f.StreamID)) + return nil +} + +// MinLength of a written frame +func (f *MaxStreamIDFrame) MinLength(protocol.VersionNumber) protocol.ByteCount { + return 1 + utils.VarIntLen(uint64(f.StreamID)) +} diff --git a/internal/wire/max_stream_id_frame_test.go b/internal/wire/max_stream_id_frame_test.go new file mode 100644 index 00000000..33a70bd7 --- /dev/null +++ b/internal/wire/max_stream_id_frame_test.go @@ -0,0 +1,51 @@ +package wire + +import ( + "bytes" + + "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/utils" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("MAX_STREAM_ID frame", func() { + Context("parsing", func() { + It("accepts sample frame", func() { + data := []byte{0x6} + data = append(data, encodeVarInt(0xdecafbad)...) + b := bytes.NewReader(data) + f, err := ParseMaxStreamIDFrame(b, protocol.VersionWhatever) + Expect(err).ToNot(HaveOccurred()) + Expect(f.StreamID).To(Equal(protocol.StreamID(0xdecafbad))) + Expect(b.Len()).To(BeZero()) + }) + + It("errors on EOFs", func() { + data := []byte{0x06} + data = append(data, encodeVarInt(0xdeadbeefcafe13)...) + _, err := ParseMaxStreamIDFrame(bytes.NewReader(data), protocol.VersionWhatever) + Expect(err).NotTo(HaveOccurred()) + for i := range data { + _, err := ParseMaxStreamIDFrame(bytes.NewReader(data[0:i]), protocol.VersionWhatever) + Expect(err).To(HaveOccurred()) + } + }) + }) + + Context("writing", func() { + It("writes a sample frame", func() { + b := &bytes.Buffer{} + frame := MaxStreamIDFrame{StreamID: 0x12345678} + frame.Write(b, protocol.VersionWhatever) + expected := []byte{0x6} + expected = append(expected, encodeVarInt(0x12345678)...) + Expect(b.Bytes()).To(Equal(expected)) + }) + + It("has the correct min length", func() { + frame := MaxStreamIDFrame{StreamID: 0x1337} + Expect(frame.MinLength(protocol.VersionWhatever)).To(Equal(1 + utils.VarIntLen(0x1337))) + }) + }) +}) diff --git a/packet_unpacker.go b/packet_unpacker.go index f97215e3..aaac146a 100644 --- a/packet_unpacker.go +++ b/packet_unpacker.go @@ -106,7 +106,11 @@ func (u *packetUnpacker) parseIETFFrame(r *bytes.Reader, typeByte byte, hdr *wir if err != nil { err = qerr.Error(qerr.InvalidWindowUpdateData, err.Error()) } - // TODO(#878): implement the MAX_STREAM_ID frame + case 0x6: + frame, err = wire.ParseMaxStreamIDFrame(r, u.version) + if err != nil { + err = qerr.Error(qerr.InvalidFrameData, err.Error()) + } case 0x7: frame, err = wire.ParsePingFrame(r, u.version) case 0x8: diff --git a/packet_unpacker_test.go b/packet_unpacker_test.go index 6d3b3914..df6a75c1 100644 --- a/packet_unpacker_test.go +++ b/packet_unpacker_test.go @@ -342,6 +342,17 @@ var _ = Describe("Packet unpacker", func() { Expect(packet.frames).To(Equal([]wire.Frame{f})) }) + It("unpacks MAX_STREAM_ID frames", func() { + f := &wire.MaxStreamIDFrame{StreamID: 0x1337} + buf := &bytes.Buffer{} + err := f.Write(buf, versionIETFFrames) + Expect(err).ToNot(HaveOccurred()) + setData(buf.Bytes()) + packet, err := unpacker.Unpack(hdrBin, hdr, data) + Expect(err).ToNot(HaveOccurred()) + Expect(packet.frames).To(Equal([]wire.Frame{f})) + }) + It("unpacks connection-level BLOCKED frames", func() { f := &wire.BlockedFrame{Offset: 0x1234} buf := &bytes.Buffer{} @@ -406,6 +417,7 @@ var _ = Describe("Packet unpacker", func() { 0x02: qerr.InvalidConnectionCloseData, 0x04: qerr.InvalidWindowUpdateData, 0x05: qerr.InvalidWindowUpdateData, + 0x06: qerr.InvalidFrameData, 0x08: qerr.InvalidBlockedData, 0x09: qerr.InvalidBlockedData, 0x0c: qerr.InvalidFrameData,