github.com/apernet/quic-go@v0.43.1-0.20240515053213-5e9e635fd9f0/internal/ackhandler/received_packet_handler_test.go (about)

     1  package ackhandler
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/apernet/quic-go/internal/protocol"
     7  	"github.com/apernet/quic-go/internal/utils"
     8  	"github.com/apernet/quic-go/internal/wire"
     9  
    10  	. "github.com/onsi/ginkgo/v2"
    11  	. "github.com/onsi/gomega"
    12  	"go.uber.org/mock/gomock"
    13  )
    14  
    15  var _ = Describe("Received Packet Handler", func() {
    16  	var handler ReceivedPacketHandler
    17  	var sentPackets *MockSentPacketTracker
    18  
    19  	BeforeEach(func() {
    20  		sentPackets = NewMockSentPacketTracker(mockCtrl)
    21  		handler = newReceivedPacketHandler(sentPackets, utils.DefaultLogger)
    22  	})
    23  
    24  	It("generates ACKs for different packet number spaces", func() {
    25  		sentPackets.EXPECT().GetLowestPacketNotConfirmedAcked().AnyTimes()
    26  		sendTime := time.Now().Add(-time.Second)
    27  		sentPackets.EXPECT().ReceivedPacket(protocol.EncryptionInitial).Times(2)
    28  		sentPackets.EXPECT().ReceivedPacket(protocol.EncryptionHandshake).Times(2)
    29  		sentPackets.EXPECT().ReceivedPacket(protocol.Encryption1RTT).Times(2)
    30  		Expect(handler.ReceivedPacket(2, protocol.ECT0, protocol.EncryptionInitial, sendTime, true)).To(Succeed())
    31  		Expect(handler.ReceivedPacket(1, protocol.ECT1, protocol.EncryptionHandshake, sendTime, true)).To(Succeed())
    32  		Expect(handler.ReceivedPacket(5, protocol.ECNCE, protocol.Encryption1RTT, sendTime, true)).To(Succeed())
    33  		Expect(handler.ReceivedPacket(3, protocol.ECT0, protocol.EncryptionInitial, sendTime, true)).To(Succeed())
    34  		Expect(handler.ReceivedPacket(2, protocol.ECT1, protocol.EncryptionHandshake, sendTime, true)).To(Succeed())
    35  		Expect(handler.ReceivedPacket(4, protocol.ECNCE, protocol.Encryption1RTT, sendTime, true)).To(Succeed())
    36  		initialAck := handler.GetAckFrame(protocol.EncryptionInitial, true)
    37  		Expect(initialAck).ToNot(BeNil())
    38  		Expect(initialAck.AckRanges).To(HaveLen(1))
    39  		Expect(initialAck.AckRanges[0]).To(Equal(wire.AckRange{Smallest: 2, Largest: 3}))
    40  		Expect(initialAck.DelayTime).To(BeZero())
    41  		Expect(initialAck.ECT0).To(BeEquivalentTo(2))
    42  		Expect(initialAck.ECT1).To(BeZero())
    43  		Expect(initialAck.ECNCE).To(BeZero())
    44  		handshakeAck := handler.GetAckFrame(protocol.EncryptionHandshake, true)
    45  		Expect(handshakeAck).ToNot(BeNil())
    46  		Expect(handshakeAck.AckRanges).To(HaveLen(1))
    47  		Expect(handshakeAck.AckRanges[0]).To(Equal(wire.AckRange{Smallest: 1, Largest: 2}))
    48  		Expect(handshakeAck.DelayTime).To(BeZero())
    49  		Expect(handshakeAck.ECT0).To(BeZero())
    50  		Expect(handshakeAck.ECT1).To(BeEquivalentTo(2))
    51  		Expect(handshakeAck.ECNCE).To(BeZero())
    52  		oneRTTAck := handler.GetAckFrame(protocol.Encryption1RTT, true)
    53  		Expect(oneRTTAck).ToNot(BeNil())
    54  		Expect(oneRTTAck.AckRanges).To(HaveLen(1))
    55  		Expect(oneRTTAck.AckRanges[0]).To(Equal(wire.AckRange{Smallest: 4, Largest: 5}))
    56  		Expect(oneRTTAck.DelayTime).To(BeNumerically("~", time.Second, 50*time.Millisecond))
    57  		Expect(oneRTTAck.ECT0).To(BeZero())
    58  		Expect(oneRTTAck.ECT1).To(BeZero())
    59  		Expect(oneRTTAck.ECNCE).To(BeEquivalentTo(2))
    60  	})
    61  
    62  	It("uses the same packet number space for 0-RTT and 1-RTT packets", func() {
    63  		sentPackets.EXPECT().GetLowestPacketNotConfirmedAcked().AnyTimes()
    64  		sentPackets.EXPECT().ReceivedPacket(protocol.Encryption0RTT)
    65  		sentPackets.EXPECT().ReceivedPacket(protocol.Encryption1RTT)
    66  		sendTime := time.Now().Add(-time.Second)
    67  		Expect(handler.ReceivedPacket(2, protocol.ECNNon, protocol.Encryption0RTT, sendTime, true)).To(Succeed())
    68  		Expect(handler.ReceivedPacket(3, protocol.ECNNon, protocol.Encryption1RTT, sendTime, true)).To(Succeed())
    69  		ack := handler.GetAckFrame(protocol.Encryption1RTT, true)
    70  		Expect(ack).ToNot(BeNil())
    71  		Expect(ack.AckRanges).To(HaveLen(1))
    72  		Expect(ack.AckRanges[0]).To(Equal(wire.AckRange{Smallest: 2, Largest: 3}))
    73  	})
    74  
    75  	It("rejects 0-RTT packets with higher packet numbers than 1-RTT packets", func() {
    76  		sentPackets.EXPECT().ReceivedPacket(gomock.Any()).Times(3)
    77  		sentPackets.EXPECT().GetLowestPacketNotConfirmedAcked().AnyTimes()
    78  		sendTime := time.Now()
    79  		Expect(handler.ReceivedPacket(10, protocol.ECNNon, protocol.Encryption0RTT, sendTime, true)).To(Succeed())
    80  		Expect(handler.ReceivedPacket(11, protocol.ECNNon, protocol.Encryption1RTT, sendTime, true)).To(Succeed())
    81  		Expect(handler.ReceivedPacket(12, protocol.ECNNon, protocol.Encryption0RTT, sendTime, true)).To(MatchError("received packet number 12 on a 0-RTT packet after receiving 11 on a 1-RTT packet"))
    82  	})
    83  
    84  	It("allows reordered 0-RTT packets", func() {
    85  		sentPackets.EXPECT().ReceivedPacket(gomock.Any()).Times(3)
    86  		sentPackets.EXPECT().GetLowestPacketNotConfirmedAcked().AnyTimes()
    87  		sendTime := time.Now()
    88  		Expect(handler.ReceivedPacket(10, protocol.ECNNon, protocol.Encryption0RTT, sendTime, true)).To(Succeed())
    89  		Expect(handler.ReceivedPacket(12, protocol.ECNNon, protocol.Encryption1RTT, sendTime, true)).To(Succeed())
    90  		Expect(handler.ReceivedPacket(11, protocol.ECNNon, protocol.Encryption0RTT, sendTime, true)).To(Succeed())
    91  	})
    92  
    93  	It("drops Initial packets", func() {
    94  		sentPackets.EXPECT().ReceivedPacket(gomock.Any()).Times(2)
    95  		sendTime := time.Now().Add(-time.Second)
    96  		Expect(handler.ReceivedPacket(2, protocol.ECNNon, protocol.EncryptionInitial, sendTime, true)).To(Succeed())
    97  		Expect(handler.ReceivedPacket(1, protocol.ECNNon, protocol.EncryptionHandshake, sendTime, true)).To(Succeed())
    98  		Expect(handler.GetAckFrame(protocol.EncryptionInitial, true)).ToNot(BeNil())
    99  		handler.DropPackets(protocol.EncryptionInitial)
   100  		Expect(handler.GetAckFrame(protocol.EncryptionInitial, true)).To(BeNil())
   101  		Expect(handler.GetAckFrame(protocol.EncryptionHandshake, true)).ToNot(BeNil())
   102  	})
   103  
   104  	It("drops Handshake packets", func() {
   105  		sentPackets.EXPECT().ReceivedPacket(gomock.Any()).Times(2)
   106  		sentPackets.EXPECT().GetLowestPacketNotConfirmedAcked().AnyTimes()
   107  		sendTime := time.Now().Add(-time.Second)
   108  		Expect(handler.ReceivedPacket(1, protocol.ECNNon, protocol.EncryptionHandshake, sendTime, true)).To(Succeed())
   109  		Expect(handler.ReceivedPacket(2, protocol.ECNNon, protocol.Encryption1RTT, sendTime, true)).To(Succeed())
   110  		Expect(handler.GetAckFrame(protocol.EncryptionHandshake, true)).ToNot(BeNil())
   111  		handler.DropPackets(protocol.EncryptionInitial)
   112  		Expect(handler.GetAckFrame(protocol.EncryptionHandshake, true)).To(BeNil())
   113  		Expect(handler.GetAckFrame(protocol.Encryption1RTT, true)).ToNot(BeNil())
   114  	})
   115  
   116  	It("does nothing when dropping 0-RTT packets", func() {
   117  		handler.DropPackets(protocol.Encryption0RTT)
   118  	})
   119  
   120  	It("drops old ACK ranges", func() {
   121  		sentPackets.EXPECT().ReceivedPacket(gomock.Any()).AnyTimes()
   122  		sendTime := time.Now()
   123  		sentPackets.EXPECT().GetLowestPacketNotConfirmedAcked().Times(2)
   124  		Expect(handler.ReceivedPacket(1, protocol.ECNNon, protocol.Encryption1RTT, sendTime, true)).To(Succeed())
   125  		Expect(handler.ReceivedPacket(2, protocol.ECNNon, protocol.Encryption1RTT, sendTime, true)).To(Succeed())
   126  		ack := handler.GetAckFrame(protocol.Encryption1RTT, true)
   127  		Expect(ack).ToNot(BeNil())
   128  		Expect(ack.LowestAcked()).To(Equal(protocol.PacketNumber(1)))
   129  		Expect(ack.LargestAcked()).To(Equal(protocol.PacketNumber(2)))
   130  		sentPackets.EXPECT().GetLowestPacketNotConfirmedAcked()
   131  		Expect(handler.ReceivedPacket(3, protocol.ECNNon, protocol.Encryption1RTT, sendTime, true)).To(Succeed())
   132  		sentPackets.EXPECT().GetLowestPacketNotConfirmedAcked().Return(protocol.PacketNumber(2))
   133  		Expect(handler.ReceivedPacket(4, protocol.ECNNon, protocol.Encryption1RTT, sendTime, true)).To(Succeed())
   134  		ack = handler.GetAckFrame(protocol.Encryption1RTT, true)
   135  		Expect(ack).ToNot(BeNil())
   136  		Expect(ack.LowestAcked()).To(Equal(protocol.PacketNumber(2)))
   137  		Expect(ack.LargestAcked()).To(Equal(protocol.PacketNumber(4)))
   138  	})
   139  
   140  	It("says if packets are duplicates", func() {
   141  		sendTime := time.Now()
   142  		sentPackets.EXPECT().ReceivedPacket(gomock.Any()).AnyTimes()
   143  		sentPackets.EXPECT().GetLowestPacketNotConfirmedAcked().AnyTimes()
   144  		// Initial
   145  		Expect(handler.IsPotentiallyDuplicate(3, protocol.EncryptionInitial)).To(BeFalse())
   146  		Expect(handler.ReceivedPacket(3, protocol.ECNNon, protocol.EncryptionInitial, sendTime, true)).To(Succeed())
   147  		Expect(handler.IsPotentiallyDuplicate(3, protocol.EncryptionInitial)).To(BeTrue())
   148  		// Handshake
   149  		Expect(handler.IsPotentiallyDuplicate(3, protocol.EncryptionHandshake)).To(BeFalse())
   150  		Expect(handler.ReceivedPacket(3, protocol.ECNNon, protocol.EncryptionHandshake, sendTime, true)).To(Succeed())
   151  		Expect(handler.IsPotentiallyDuplicate(3, protocol.EncryptionHandshake)).To(BeTrue())
   152  		// 0-RTT
   153  		Expect(handler.IsPotentiallyDuplicate(3, protocol.Encryption0RTT)).To(BeFalse())
   154  		Expect(handler.ReceivedPacket(3, protocol.ECNNon, protocol.Encryption0RTT, sendTime, true)).To(Succeed())
   155  		Expect(handler.IsPotentiallyDuplicate(3, protocol.Encryption0RTT)).To(BeTrue())
   156  		// 1-RTT
   157  		Expect(handler.IsPotentiallyDuplicate(3, protocol.Encryption1RTT)).To(BeTrue())
   158  		Expect(handler.IsPotentiallyDuplicate(4, protocol.Encryption1RTT)).To(BeFalse())
   159  		Expect(handler.ReceivedPacket(4, protocol.ECNNon, protocol.Encryption1RTT, sendTime, true)).To(Succeed())
   160  		Expect(handler.IsPotentiallyDuplicate(4, protocol.Encryption1RTT)).To(BeTrue())
   161  	})
   162  })