route/vendor/github.com/lucas-clemente/quic-go/stream_framer_test.go

351 lines
12 KiB
Go
Raw Normal View History

2017-12-12 02:51:45 +00:00
package quic
import (
"bytes"
2018-01-03 19:19:49 +00:00
"github.com/golang/mock/gomock"
"github.com/lucas-clemente/quic-go/internal/mocks"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/wire"
2017-12-12 02:51:45 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Stream Framer", func() {
2018-01-03 19:19:49 +00:00
const (
id1 = protocol.StreamID(10)
id2 = protocol.StreamID(11)
)
2017-12-12 02:51:45 +00:00
var (
2018-01-03 19:19:49 +00:00
retransmittedFrame1, retransmittedFrame2 *wire.StreamFrame
2017-12-12 02:51:45 +00:00
framer *streamFramer
streamsMap *streamsMap
2018-01-03 19:19:49 +00:00
stream1, stream2 *mocks.MockStreamI
connFC *mocks.MockConnectionFlowController
2017-12-12 02:51:45 +00:00
)
BeforeEach(func() {
2018-01-03 19:19:49 +00:00
retransmittedFrame1 = &wire.StreamFrame{
2017-12-12 02:51:45 +00:00
StreamID: 5,
Data: []byte{0x13, 0x37},
}
2018-01-03 19:19:49 +00:00
retransmittedFrame2 = &wire.StreamFrame{
2017-12-12 02:51:45 +00:00
StreamID: 6,
Data: []byte{0xDE, 0xCA, 0xFB, 0xAD},
}
2018-01-03 19:19:49 +00:00
stream1 = mocks.NewMockStreamI(mockCtrl)
stream1.EXPECT().StreamID().Return(protocol.StreamID(5)).AnyTimes()
stream2 = mocks.NewMockStreamI(mockCtrl)
stream2.EXPECT().StreamID().Return(protocol.StreamID(6)).AnyTimes()
2017-12-12 02:51:45 +00:00
2018-01-03 19:19:49 +00:00
streamsMap = newStreamsMap(nil, protocol.PerspectiveServer, versionGQUICFrames)
2017-12-12 02:51:45 +00:00
streamsMap.putStream(stream1)
streamsMap.putStream(stream2)
2018-01-03 19:19:49 +00:00
connFC = mocks.NewMockConnectionFlowController(mockCtrl)
framer = newStreamFramer(nil, streamsMap, connFC, versionGQUICFrames)
2017-12-12 02:51:45 +00:00
})
2018-01-03 19:19:49 +00:00
setNoData := func(str *mocks.MockStreamI) {
str.EXPECT().HasDataForWriting().Return(false).AnyTimes()
str.EXPECT().GetDataForWriting(gomock.Any()).Return(nil, false).AnyTimes()
str.EXPECT().GetWriteOffset().AnyTimes()
}
2017-12-12 02:51:45 +00:00
It("says if it has retransmissions", func() {
Expect(framer.HasFramesForRetransmission()).To(BeFalse())
framer.AddFrameForRetransmission(retransmittedFrame1)
Expect(framer.HasFramesForRetransmission()).To(BeTrue())
})
It("sets the DataLenPresent for dequeued retransmitted frames", func() {
2018-01-03 19:19:49 +00:00
setNoData(stream1)
setNoData(stream2)
2017-12-12 02:51:45 +00:00
framer.AddFrameForRetransmission(retransmittedFrame1)
fs := framer.PopStreamFrames(protocol.MaxByteCount)
Expect(fs).To(HaveLen(1))
Expect(fs[0].DataLenPresent).To(BeTrue())
})
It("sets the DataLenPresent for dequeued normal frames", func() {
2018-01-03 19:19:49 +00:00
connFC.EXPECT().IsBlocked()
setNoData(stream2)
stream1.EXPECT().GetWriteOffset()
stream1.EXPECT().HasDataForWriting().Return(true)
stream1.EXPECT().GetDataForWriting(gomock.Any()).Return([]byte("foobar"), false)
stream1.EXPECT().IsFlowControlBlocked()
2017-12-12 02:51:45 +00:00
fs := framer.PopStreamFrames(protocol.MaxByteCount)
Expect(fs).To(HaveLen(1))
Expect(fs[0].DataLenPresent).To(BeTrue())
})
Context("Popping", func() {
2018-01-03 19:19:49 +00:00
BeforeEach(func() {
// nothing is blocked here
connFC.EXPECT().IsBlocked().AnyTimes()
stream1.EXPECT().IsFlowControlBlocked().Return(false).AnyTimes()
stream2.EXPECT().IsFlowControlBlocked().Return(false).AnyTimes()
})
2017-12-12 02:51:45 +00:00
It("returns nil when popping an empty framer", func() {
2018-01-03 19:19:49 +00:00
setNoData(stream1)
setNoData(stream2)
2017-12-12 02:51:45 +00:00
Expect(framer.PopStreamFrames(1000)).To(BeEmpty())
})
It("pops frames for retransmission", func() {
2018-01-03 19:19:49 +00:00
setNoData(stream1)
setNoData(stream2)
2017-12-12 02:51:45 +00:00
framer.AddFrameForRetransmission(retransmittedFrame1)
framer.AddFrameForRetransmission(retransmittedFrame2)
fs := framer.PopStreamFrames(1000)
Expect(fs).To(HaveLen(2))
Expect(fs[0]).To(Equal(retransmittedFrame1))
Expect(fs[1]).To(Equal(retransmittedFrame2))
Expect(framer.PopStreamFrames(1000)).To(BeEmpty())
})
It("returns normal frames", func() {
2018-01-03 19:19:49 +00:00
stream1.EXPECT().GetDataForWriting(gomock.Any()).Return([]byte("foobar"), false)
stream1.EXPECT().HasDataForWriting().Return(true)
stream1.EXPECT().GetWriteOffset()
setNoData(stream2)
2017-12-12 02:51:45 +00:00
fs := framer.PopStreamFrames(1000)
Expect(fs).To(HaveLen(1))
2018-01-03 19:19:49 +00:00
Expect(fs[0].StreamID).To(Equal(stream1.StreamID()))
2017-12-12 02:51:45 +00:00
Expect(fs[0].Data).To(Equal([]byte("foobar")))
2018-01-03 19:19:49 +00:00
Expect(fs[0].FinBit).To(BeFalse())
2017-12-12 02:51:45 +00:00
})
It("returns multiple normal frames", func() {
2018-01-03 19:19:49 +00:00
stream1.EXPECT().GetDataForWriting(gomock.Any()).Return([]byte("foobar"), false)
stream1.EXPECT().HasDataForWriting().Return(true)
stream1.EXPECT().GetWriteOffset()
stream2.EXPECT().GetDataForWriting(gomock.Any()).Return([]byte("foobaz"), false)
stream2.EXPECT().HasDataForWriting().Return(true)
stream2.EXPECT().GetWriteOffset()
2017-12-12 02:51:45 +00:00
fs := framer.PopStreamFrames(1000)
Expect(fs).To(HaveLen(2))
// Swap if we dequeued in other order
2018-01-03 19:19:49 +00:00
if fs[0].StreamID != stream1.StreamID() {
2017-12-12 02:51:45 +00:00
fs[0], fs[1] = fs[1], fs[0]
}
2018-01-03 19:19:49 +00:00
Expect(fs[0].StreamID).To(Equal(stream1.StreamID()))
2017-12-12 02:51:45 +00:00
Expect(fs[0].Data).To(Equal([]byte("foobar")))
2018-01-03 19:19:49 +00:00
Expect(fs[1].StreamID).To(Equal(stream2.StreamID()))
2017-12-12 02:51:45 +00:00
Expect(fs[1].Data).To(Equal([]byte("foobaz")))
})
It("returns retransmission frames before normal frames", func() {
2018-01-03 19:19:49 +00:00
stream1.EXPECT().GetDataForWriting(gomock.Any()).Return([]byte("foobar"), false)
stream1.EXPECT().HasDataForWriting().Return(true)
stream1.EXPECT().GetWriteOffset()
setNoData(stream2)
2017-12-12 02:51:45 +00:00
framer.AddFrameForRetransmission(retransmittedFrame1)
fs := framer.PopStreamFrames(1000)
Expect(fs).To(HaveLen(2))
Expect(fs[0]).To(Equal(retransmittedFrame1))
2018-01-03 19:19:49 +00:00
Expect(fs[1].StreamID).To(Equal(stream1.StreamID()))
2017-12-12 02:51:45 +00:00
})
It("does not pop empty frames", func() {
2018-01-03 19:19:49 +00:00
stream1.EXPECT().HasDataForWriting().Return(false)
stream1.EXPECT().GetWriteOffset()
setNoData(stream2)
fs := framer.PopStreamFrames(5)
Expect(fs).To(BeEmpty())
2017-12-12 02:51:45 +00:00
})
It("uses the round-robin scheduling", func() {
2018-01-03 19:19:49 +00:00
streamFrameHeaderLen := protocol.ByteCount(4)
stream1.EXPECT().GetDataForWriting(10-streamFrameHeaderLen).Return(bytes.Repeat([]byte("f"), int(10-streamFrameHeaderLen)), false)
stream1.EXPECT().HasDataForWriting().Return(true)
stream1.EXPECT().GetWriteOffset()
stream2.EXPECT().GetDataForWriting(protocol.ByteCount(10-streamFrameHeaderLen)).Return(bytes.Repeat([]byte("e"), int(10-streamFrameHeaderLen)), false)
stream2.EXPECT().HasDataForWriting().Return(true)
stream2.EXPECT().GetWriteOffset()
2017-12-12 02:51:45 +00:00
fs := framer.PopStreamFrames(10)
Expect(fs).To(HaveLen(1))
// it doesn't matter here if this data is from stream1 or from stream2...
firstStreamID := fs[0].StreamID
fs = framer.PopStreamFrames(10)
Expect(fs).To(HaveLen(1))
// ... but the data popped this time has to be from the other stream
Expect(fs[0].StreamID).ToNot(Equal(firstStreamID))
})
Context("splitting of frames", func() {
It("splits off nothing", func() {
2018-01-03 19:19:49 +00:00
f := &wire.StreamFrame{
2017-12-12 02:51:45 +00:00
StreamID: 1,
Data: []byte("bar"),
Offset: 3,
}
Expect(maybeSplitOffFrame(f, 1000)).To(BeNil())
Expect(f.Offset).To(Equal(protocol.ByteCount(3)))
Expect(f.Data).To(Equal([]byte("bar")))
})
It("splits off initial frame", func() {
2018-01-03 19:19:49 +00:00
f := &wire.StreamFrame{
2017-12-12 02:51:45 +00:00
StreamID: 1,
Data: []byte("foobar"),
DataLenPresent: true,
Offset: 3,
FinBit: true,
}
previous := maybeSplitOffFrame(f, 3)
Expect(previous).ToNot(BeNil())
Expect(previous.StreamID).To(Equal(protocol.StreamID(1)))
Expect(previous.Data).To(Equal([]byte("foo")))
Expect(previous.DataLenPresent).To(BeTrue())
Expect(previous.Offset).To(Equal(protocol.ByteCount(3)))
Expect(previous.FinBit).To(BeFalse())
Expect(f.StreamID).To(Equal(protocol.StreamID(1)))
Expect(f.Data).To(Equal([]byte("bar")))
Expect(f.DataLenPresent).To(BeTrue())
Expect(f.Offset).To(Equal(protocol.ByteCount(6)))
Expect(f.FinBit).To(BeTrue())
})
It("splits a frame", func() {
2018-01-03 19:19:49 +00:00
setNoData(stream1)
setNoData(stream2)
2017-12-12 02:51:45 +00:00
framer.AddFrameForRetransmission(retransmittedFrame2)
origlen := retransmittedFrame2.DataLen()
fs := framer.PopStreamFrames(6)
Expect(fs).To(HaveLen(1))
2018-01-03 19:19:49 +00:00
minLength, _ := fs[0].MinLength(framer.version)
2017-12-12 02:51:45 +00:00
Expect(minLength + fs[0].DataLen()).To(Equal(protocol.ByteCount(6)))
Expect(framer.retransmissionQueue[0].Data).To(HaveLen(int(origlen - fs[0].DataLen())))
Expect(framer.retransmissionQueue[0].Offset).To(Equal(fs[0].DataLen()))
})
It("never returns an empty stream frame", func() {
// this one frame will be split off from again and again in this test. Therefore, it has to be large enough (checked again at the end)
2018-01-03 19:19:49 +00:00
origFrame := &wire.StreamFrame{
2017-12-12 02:51:45 +00:00
StreamID: 5,
Offset: 1,
FinBit: false,
Data: bytes.Repeat([]byte{'f'}, 30*30),
}
framer.AddFrameForRetransmission(origFrame)
2018-01-03 19:19:49 +00:00
minFrameDataLen := protocol.MaxPacketSize
2017-12-12 02:51:45 +00:00
for i := 0; i < 30; i++ {
frames, currentLen := framer.maybePopFramesForRetransmission(protocol.ByteCount(i))
if len(frames) == 0 {
Expect(currentLen).To(BeZero())
} else {
Expect(frames).To(HaveLen(1))
Expect(currentLen).ToNot(BeZero())
dataLen := frames[0].DataLen()
Expect(dataLen).ToNot(BeZero())
if dataLen < minFrameDataLen {
minFrameDataLen = dataLen
}
}
}
Expect(framer.retransmissionQueue).To(HaveLen(1)) // check that origFrame was large enough for this test and didn't get used up completely
Expect(minFrameDataLen).To(Equal(protocol.ByteCount(1)))
})
It("only removes a frame from the framer after returning all split parts", func() {
2018-01-03 19:19:49 +00:00
setNoData(stream1)
setNoData(stream2)
2017-12-12 02:51:45 +00:00
framer.AddFrameForRetransmission(retransmittedFrame2)
fs := framer.PopStreamFrames(6)
Expect(fs).To(HaveLen(1))
Expect(framer.retransmissionQueue).ToNot(BeEmpty())
fs = framer.PopStreamFrames(1000)
Expect(fs).To(HaveLen(1))
Expect(framer.retransmissionQueue).To(BeEmpty())
})
})
Context("sending FINs", func() {
It("sends FINs when streams are closed", func() {
2018-01-03 19:19:49 +00:00
offset := protocol.ByteCount(42)
stream1.EXPECT().HasDataForWriting().Return(true)
stream1.EXPECT().GetDataForWriting(gomock.Any()).Return(nil, true)
stream1.EXPECT().GetWriteOffset().Return(offset)
setNoData(stream2)
2017-12-12 02:51:45 +00:00
fs := framer.PopStreamFrames(1000)
Expect(fs).To(HaveLen(1))
2018-01-03 19:19:49 +00:00
Expect(fs[0].StreamID).To(Equal(stream1.StreamID()))
Expect(fs[0].Offset).To(Equal(offset))
2017-12-12 02:51:45 +00:00
Expect(fs[0].FinBit).To(BeTrue())
Expect(fs[0].Data).To(BeEmpty())
})
It("bundles FINs with data", func() {
2018-01-03 19:19:49 +00:00
offset := protocol.ByteCount(42)
stream1.EXPECT().GetDataForWriting(gomock.Any()).Return([]byte("foobar"), true)
stream1.EXPECT().HasDataForWriting().Return(true)
stream1.EXPECT().GetWriteOffset().Return(offset)
setNoData(stream2)
2017-12-12 02:51:45 +00:00
fs := framer.PopStreamFrames(1000)
Expect(fs).To(HaveLen(1))
2018-01-03 19:19:49 +00:00
Expect(fs[0].StreamID).To(Equal(stream1.StreamID()))
2017-12-12 02:51:45 +00:00
Expect(fs[0].Data).To(Equal([]byte("foobar")))
Expect(fs[0].FinBit).To(BeTrue())
})
})
})
Context("BLOCKED frames", func() {
It("Pop returns nil if no frame is queued", func() {
Expect(framer.PopBlockedFrame()).To(BeNil())
})
It("queues and pops BLOCKED frames for individually blocked streams", func() {
2018-01-03 19:19:49 +00:00
connFC.EXPECT().IsBlocked()
stream1.EXPECT().GetDataForWriting(gomock.Any()).Return([]byte("foobar"), false)
stream1.EXPECT().HasDataForWriting().Return(true)
stream1.EXPECT().GetWriteOffset()
stream1.EXPECT().IsFlowControlBlocked().Return(true)
setNoData(stream2)
2017-12-12 02:51:45 +00:00
frames := framer.PopStreamFrames(1000)
Expect(frames).To(HaveLen(1))
2018-01-03 19:19:49 +00:00
f := framer.PopBlockedFrame()
Expect(f).To(BeAssignableToTypeOf(&wire.StreamBlockedFrame{}))
bf := f.(*wire.StreamBlockedFrame)
Expect(bf.StreamID).To(Equal(stream1.StreamID()))
2017-12-12 02:51:45 +00:00
Expect(framer.PopBlockedFrame()).To(BeNil())
})
It("does not queue a stream-level BLOCKED frame after sending the FinBit frame", func() {
2018-01-03 19:19:49 +00:00
connFC.EXPECT().IsBlocked()
stream1.EXPECT().GetDataForWriting(gomock.Any()).Return([]byte("foo"), true)
stream1.EXPECT().HasDataForWriting().Return(true)
stream1.EXPECT().GetWriteOffset()
setNoData(stream2)
2017-12-12 02:51:45 +00:00
frames := framer.PopStreamFrames(1000)
Expect(frames).To(HaveLen(1))
Expect(frames[0].FinBit).To(BeTrue())
2018-01-03 19:19:49 +00:00
Expect(frames[0].DataLen()).To(Equal(protocol.ByteCount(3)))
2017-12-12 02:51:45 +00:00
blockedFrame := framer.PopBlockedFrame()
Expect(blockedFrame).To(BeNil())
})
It("queues and pops BLOCKED frames for connection blocked streams", func() {
2018-01-03 19:19:49 +00:00
connFC.EXPECT().IsBlocked().Return(true)
stream1.EXPECT().GetDataForWriting(gomock.Any()).Return([]byte("foo"), false)
stream1.EXPECT().HasDataForWriting().Return(true)
stream1.EXPECT().GetWriteOffset()
stream1.EXPECT().IsFlowControlBlocked().Return(false)
setNoData(stream2)
2017-12-12 02:51:45 +00:00
framer.PopStreamFrames(1000)
2018-01-03 19:19:49 +00:00
f := framer.PopBlockedFrame()
Expect(f).To(BeAssignableToTypeOf(&wire.BlockedFrame{}))
2017-12-12 02:51:45 +00:00
Expect(framer.PopBlockedFrame()).To(BeNil())
})
})
})