github.com/tumi8/quic-go@v0.37.4-tum/noninternal/ackhandler/received_packet_handler_test.go (about)

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