github.com/MerlinKodo/quic-go@v0.39.2/internal/ackhandler/received_packet_handler_test.go (about)

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