append to a given buffer when packing a packet

This commit is contained in:
Marten Seemann 2023-04-30 14:52:11 +02:00
parent b0524c1241
commit 628ba87727
5 changed files with 118 additions and 107 deletions

View file

@ -53,10 +53,10 @@ var _ = Describe("Connection", func() {
destConnID := protocol.ParseConnectionID([]byte{8, 7, 6, 5, 4, 3, 2, 1})
clientDestConnID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
getShortHeaderPacket := func(pn protocol.PacketNumber) (shortHeaderPacket, *packetBuffer) {
getShortHeaderPacket := func(pn protocol.PacketNumber) shortHeaderPacket {
buffer := getPacketBuffer()
buffer.Data = append(buffer.Data, []byte("foobar")...)
return shortHeaderPacket{Packet: &ackhandler.Packet{PacketNumber: pn}}, buffer
return shortHeaderPacket{Packet: &ackhandler.Packet{PacketNumber: pn}}
}
getCoalescedPacket := func(pn protocol.PacketNumber, isLongHeader bool) *coalescedPacket {
@ -613,9 +613,9 @@ var _ = Describe("Connection", func() {
connRunner.EXPECT().Remove(gomock.Any()).AnyTimes()
cryptoSetup.EXPECT().Close()
conn.sentPacketHandler = sph
p, buffer := getShortHeaderPacket(1)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack).AnyTimes()
p := getShortHeaderPacket(1)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack).AnyTimes()
runConn()
conn.queueControlFrame(&wire.PingFrame{})
conn.scheduleSending()
@ -1208,9 +1208,9 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SentPacket(gomock.Any())
conn.sentPacketHandler = sph
runConn()
p, buffer := getShortHeaderPacket(1)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack).AnyTimes()
p := getShortHeaderPacket(1)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack).AnyTimes()
sent := make(chan struct{})
sender.EXPECT().WouldBlock().AnyTimes()
sender.EXPECT().Send(gomock.Any()).Do(func(packet *packetBuffer) { close(sent) })
@ -1219,7 +1219,7 @@ var _ = Describe("Connection", func() {
PacketNumber: p.PacketNumber,
PacketNumberLen: p.PacketNumberLen,
KeyPhase: p.KeyPhase,
}, buffer.Len(), nil, []logging.Frame{})
}, gomock.Any(), nil, []logging.Frame{})
conn.scheduleSending()
Eventually(sent).Should(BeClosed())
})
@ -1227,7 +1227,7 @@ var _ = Describe("Connection", func() {
It("doesn't send packets if there's nothing to send", func() {
conn.handshakeConfirmed = true
runConn()
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack).AnyTimes()
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack).AnyTimes()
conn.receivedPacketHandler.ReceivedPacket(0x035e, protocol.ECNNon, protocol.Encryption1RTT, time.Now(), true)
conn.scheduleSending()
time.Sleep(50 * time.Millisecond) // make sure there are no calls to mconn.Write()
@ -1256,14 +1256,14 @@ var _ = Describe("Connection", func() {
conn.sentPacketHandler = sph
fc := mocks.NewMockConnectionFlowController(mockCtrl)
fc.EXPECT().IsNewlyBlocked().Return(true, protocol.ByteCount(1337))
p, buffer := getShortHeaderPacket(1)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack).AnyTimes()
p := getShortHeaderPacket(1)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack).AnyTimes()
conn.connFlowController = fc
runConn()
sent := make(chan struct{})
sender.EXPECT().Send(gomock.Any()).Do(func(packet *packetBuffer) { close(sent) })
tracer.EXPECT().SentShortHeaderPacket(gomock.Any(), buffer.Len(), nil, []logging.Frame{})
tracer.EXPECT().SentShortHeaderPacket(gomock.Any(), gomock.Any(), nil, []logging.Frame{})
conn.scheduleSending()
Eventually(sent).Should(BeClosed())
frames, _ := conn.framer.AppendControlFrames(nil, 1000, protocol.Version1)
@ -1396,10 +1396,10 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SendMode().Return(ackhandler.SendAny).Times(2)
sph.EXPECT().SendMode().Return(ackhandler.SendPacingLimited)
sph.EXPECT().TimeUntilSend().Return(time.Now().Add(time.Hour))
p, buffer := getShortHeaderPacket(10)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
p, buffer = getShortHeaderPacket(11)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
p := getShortHeaderPacket(10)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
p = getShortHeaderPacket(11)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
sender.EXPECT().WouldBlock().AnyTimes()
sender.EXPECT().Send(gomock.Any()).Times(2)
go func() {
@ -1414,9 +1414,9 @@ var _ = Describe("Connection", func() {
It("sends multiple packets, when the pacer allows immediate sending", func() {
sph.EXPECT().SentPacket(gomock.Any())
sph.EXPECT().SendMode().Return(ackhandler.SendAny).Times(2)
p, buffer := getShortHeaderPacket(10)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack)
p := getShortHeaderPacket(10)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack)
sender.EXPECT().WouldBlock().AnyTimes()
sender.EXPECT().Send(gomock.Any())
go func() {
@ -1432,8 +1432,8 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SentPacket(gomock.Any())
sph.EXPECT().TimeUntilSend().Return(time.Now().Add(time.Hour))
sph.EXPECT().SendMode().Return(ackhandler.SendPacingLimited)
p, buffer := getShortHeaderPacket(10)
packer.EXPECT().PackAckOnlyPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
p := getShortHeaderPacket(10)
packer.EXPECT().PackAckOnlyPacket(gomock.Any(), conn.version).Return(p, getPacketBuffer(), nil)
sender.EXPECT().WouldBlock().AnyTimes()
sender.EXPECT().Send(gomock.Any())
@ -1452,8 +1452,8 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SentPacket(gomock.Any())
sph.EXPECT().SendMode().Return(ackhandler.SendAny)
sph.EXPECT().SendMode().Return(ackhandler.SendAck)
p, buffer := getShortHeaderPacket(100)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
p := getShortHeaderPacket(100)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
sender.EXPECT().WouldBlock().AnyTimes()
sender.EXPECT().Send(gomock.Any())
go func() {
@ -1467,16 +1467,16 @@ var _ = Describe("Connection", func() {
It("paces packets", func() {
pacingDelay := scaleDuration(100 * time.Millisecond)
p1, buffer1 := getShortHeaderPacket(100)
p2, buffer2 := getShortHeaderPacket(101)
p1 := getShortHeaderPacket(100)
p2 := getShortHeaderPacket(101)
gomock.InOrder(
sph.EXPECT().SendMode().Return(ackhandler.SendAny),
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p1, buffer1, nil),
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p1, nil),
sph.EXPECT().SentPacket(gomock.Any()),
sph.EXPECT().SendMode().Return(ackhandler.SendPacingLimited),
sph.EXPECT().TimeUntilSend().Return(time.Now().Add(pacingDelay)),
sph.EXPECT().SendMode().Return(ackhandler.SendAny),
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p2, buffer2, nil),
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p2, nil),
sph.EXPECT().SentPacket(gomock.Any()),
sph.EXPECT().SendMode().Return(ackhandler.SendPacingLimited),
sph.EXPECT().TimeUntilSend().Return(time.Now().Add(time.Hour)),
@ -1501,8 +1501,8 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SendMode().Return(ackhandler.SendPacingLimited)
sph.EXPECT().TimeUntilSend().Return(time.Now().Add(time.Hour))
for pn := protocol.PacketNumber(1000); pn < 1003; pn++ {
p, buffer := getShortHeaderPacket(pn)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
p := getShortHeaderPacket(pn)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
}
written := make(chan struct{}, 3)
sender.EXPECT().WouldBlock().AnyTimes()
@ -1532,9 +1532,9 @@ var _ = Describe("Connection", func() {
sender.EXPECT().WouldBlock().AnyTimes()
sph.EXPECT().SentPacket(gomock.Any())
sph.EXPECT().SendMode().Return(ackhandler.SendAny).AnyTimes()
p, buffer := getShortHeaderPacket(1000)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack)
p := getShortHeaderPacket(1000)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack)
sender.EXPECT().Send(gomock.Any()).DoAndReturn(func(p *packetBuffer) { close(written) })
available <- struct{}{}
Eventually(written).Should(BeClosed())
@ -1555,9 +1555,9 @@ var _ = Describe("Connection", func() {
conn.handlePacket(&receivedPacket{buffer: getPacketBuffer()})
})
sph.EXPECT().SendMode().Return(ackhandler.SendAny).AnyTimes()
p, buffer := getShortHeaderPacket(1000)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack)
p := getShortHeaderPacket(1000)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack)
sender.EXPECT().Send(gomock.Any()).DoAndReturn(func(p *packetBuffer) { close(written) })
conn.scheduleSending()
@ -1569,8 +1569,8 @@ var _ = Describe("Connection", func() {
It("stops sending when the send queue is full", func() {
sph.EXPECT().SentPacket(gomock.Any())
sph.EXPECT().SendMode().Return(ackhandler.SendAny)
p, buffer := getShortHeaderPacket(1000)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
p := getShortHeaderPacket(1000)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
written := make(chan struct{}, 1)
sender.EXPECT().WouldBlock()
sender.EXPECT().WouldBlock().Return(true).Times(2)
@ -1590,9 +1590,9 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SentPacket(gomock.Any())
sph.EXPECT().SendMode().Return(ackhandler.SendAny).AnyTimes()
sender.EXPECT().WouldBlock().AnyTimes()
p, buffer = getShortHeaderPacket(1001)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack)
p = getShortHeaderPacket(1001)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack)
sender.EXPECT().Send(gomock.Any()).DoAndReturn(func(p *packetBuffer) { written <- struct{}{} })
available <- struct{}{}
Eventually(written).Should(Receive())
@ -1605,7 +1605,7 @@ var _ = Describe("Connection", func() {
It("doesn't set a pacing timer when there is no data to send", func() {
sph.EXPECT().SendMode().Return(ackhandler.SendAny).AnyTimes()
sender.EXPECT().WouldBlock().AnyTimes()
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack)
// don't EXPECT any calls to mconn.Write()
go func() {
defer GinkgoRecover()
@ -1629,8 +1629,8 @@ var _ = Describe("Connection", func() {
mtuDiscoverer.EXPECT().ShouldSendProbe(gomock.Any()).Return(true)
ping := ackhandler.Frame{Frame: &wire.PingFrame{}}
mtuDiscoverer.EXPECT().GetPing().Return(ping, protocol.ByteCount(1234))
p, buffer := getShortHeaderPacket(1)
packer.EXPECT().PackMTUProbePacket(ping, protocol.ByteCount(1234), gomock.Any(), conn.version).Return(p, buffer, nil)
p := getShortHeaderPacket(1)
packer.EXPECT().PackMTUProbePacket(ping, protocol.ByteCount(1234), gomock.Any(), conn.version).Return(p, getPacketBuffer(), nil)
go func() {
defer GinkgoRecover()
cryptoSetup.EXPECT().RunHandshake().MaxTimes(1)
@ -1675,9 +1675,9 @@ var _ = Describe("Connection", func() {
sph.EXPECT().SentPacket(gomock.Any())
conn.sentPacketHandler = sph
p, buffer := getShortHeaderPacket(1)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack)
p := getShortHeaderPacket(1)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack)
go func() {
defer GinkgoRecover()
@ -1695,9 +1695,9 @@ var _ = Describe("Connection", func() {
})
It("sets the timer to the ack timer", func() {
p, buffer := getShortHeaderPacket(1234)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(p, buffer, nil)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack)
p := getShortHeaderPacket(1234)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(p, nil)
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack)
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().GetLossDetectionTimeout().AnyTimes()
sph.EXPECT().SendMode().Return(ackhandler.SendAny).AnyTimes()
@ -1912,14 +1912,14 @@ var _ = Describe("Connection", func() {
conn.sentPacketHandler = sph
done := make(chan struct{})
connRunner.EXPECT().Retire(clientDestConnID)
packer.EXPECT().PackPacket(gomock.Any(), conn.version).DoAndReturn(func(_ protocol.ByteCount, v protocol.VersionNumber) (shortHeaderPacket, *packetBuffer, error) {
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).DoAndReturn(func(_ *packetBuffer, _ protocol.ByteCount, v protocol.VersionNumber) (shortHeaderPacket, error) {
frames, _ := conn.framer.AppendControlFrames(nil, protocol.MaxByteCount, v)
Expect(frames).ToNot(BeEmpty())
Expect(frames[0].Frame).To(BeEquivalentTo(&wire.HandshakeDoneFrame{}))
defer close(done)
return shortHeaderPacket{Packet: &ackhandler.Packet{}}, getPacketBuffer(), nil
return shortHeaderPacket{Packet: &ackhandler.Packet{}}, nil
})
packer.EXPECT().PackPacket(gomock.Any(), conn.version).Return(shortHeaderPacket{}, nil, errNothingToPack).AnyTimes()
packer.EXPECT().AppendPacket(gomock.Any(), gomock.Any(), conn.version).Return(shortHeaderPacket{}, errNothingToPack).AnyTimes()
go func() {
defer GinkgoRecover()
cryptoSetup.EXPECT().RunHandshake()