github.com/metacubex/quic-go@v0.44.1-0.20240520163451-20b689a59136/internal/ackhandler/sent_packet_handler_test.go (about) 1 package ackhandler 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/metacubex/quic-go/internal/mocks" 8 "github.com/metacubex/quic-go/internal/protocol" 9 "github.com/metacubex/quic-go/internal/qerr" 10 "github.com/metacubex/quic-go/internal/utils" 11 "github.com/metacubex/quic-go/internal/wire" 12 13 . "github.com/onsi/ginkgo/v2" 14 . "github.com/onsi/gomega" 15 "go.uber.org/mock/gomock" 16 ) 17 18 type customFrameHandler struct { 19 onLost, onAcked func(wire.Frame) 20 } 21 22 func (h *customFrameHandler) OnLost(f wire.Frame) { 23 if h.onLost != nil { 24 h.onLost(f) 25 } 26 } 27 28 func (h *customFrameHandler) OnAcked(f wire.Frame) { 29 if h.onAcked != nil { 30 h.onAcked(f) 31 } 32 } 33 34 var _ = Describe("SentPacketHandler", func() { 35 var ( 36 handler *sentPacketHandler 37 streamFrame wire.StreamFrame 38 lostPackets []protocol.PacketNumber 39 perspective protocol.Perspective 40 ) 41 42 BeforeEach(func() { perspective = protocol.PerspectiveServer }) 43 44 JustBeforeEach(func() { 45 lostPackets = nil 46 rttStats := utils.NewRTTStats() 47 handler = newSentPacketHandler(42, protocol.InitialPacketSize, rttStats, false, false, perspective, nil, utils.DefaultLogger) 48 streamFrame = wire.StreamFrame{ 49 StreamID: 5, 50 Data: []byte{0x13, 0x37}, 51 } 52 }) 53 54 getPacket := func(pn protocol.PacketNumber, encLevel protocol.EncryptionLevel) *packet { 55 for _, p := range handler.getPacketNumberSpace(encLevel).history.packets { 56 if p != nil && p.PacketNumber == pn { 57 return p 58 } 59 } 60 return nil 61 } 62 63 ackElicitingPacket := func(p *packet) *packet { 64 if p.EncryptionLevel == 0 { 65 p.EncryptionLevel = protocol.Encryption1RTT 66 } 67 if p.Length == 0 { 68 p.Length = 1 69 } 70 if p.SendTime.IsZero() { 71 p.SendTime = time.Now() 72 } 73 if len(p.Frames) == 0 { 74 p.Frames = []Frame{ 75 {Frame: &wire.PingFrame{}, Handler: &customFrameHandler{ 76 onLost: func(wire.Frame) { lostPackets = append(lostPackets, p.PacketNumber) }, 77 }}, 78 } 79 } 80 return p 81 } 82 83 nonAckElicitingPacket := func(p *packet) *packet { 84 p = ackElicitingPacket(p) 85 p.Frames = nil 86 p.LargestAcked = 1 87 return p 88 } 89 90 initialPacket := func(p *packet) *packet { 91 p = ackElicitingPacket(p) 92 p.EncryptionLevel = protocol.EncryptionInitial 93 return p 94 } 95 96 handshakePacket := func(p *packet) *packet { 97 p = ackElicitingPacket(p) 98 p.EncryptionLevel = protocol.EncryptionHandshake 99 return p 100 } 101 102 handshakePacketNonAckEliciting := func(p *packet) *packet { 103 p = nonAckElicitingPacket(p) 104 p.EncryptionLevel = protocol.EncryptionHandshake 105 return p 106 } 107 108 sentPacket := func(p *packet) { 109 handler.SentPacket(p.SendTime, p.PacketNumber, p.LargestAcked, p.StreamFrames, p.Frames, p.EncryptionLevel, protocol.ECNNon, p.Length, p.IsPathMTUProbePacket) 110 } 111 112 expectInPacketHistory := func(expected []protocol.PacketNumber, encLevel protocol.EncryptionLevel) { 113 pnSpace := handler.getPacketNumberSpace(encLevel) 114 var length int 115 pnSpace.history.Iterate(func(p *packet) (bool, error) { 116 if !p.declaredLost && !p.skippedPacket { 117 length++ 118 } 119 return true, nil 120 }) 121 ExpectWithOffset(1, length).To(Equal(len(expected))) 122 for _, p := range expected { 123 ExpectWithOffset(2, getPacket(p, encLevel)).ToNot(BeNil()) 124 } 125 } 126 127 updateRTT := func(rtt time.Duration) { 128 handler.rttStats.UpdateRTT(rtt, 0, time.Now()) 129 ExpectWithOffset(1, handler.rttStats.SmoothedRTT()).To(Equal(rtt)) 130 } 131 132 // setHandshakeConfirmed drops both Initial and Handshake packets and then confirms the handshake 133 setHandshakeConfirmed := func() { 134 handler.DropPackets(protocol.EncryptionInitial) 135 handler.DropPackets(protocol.EncryptionHandshake) 136 handler.SetHandshakeConfirmed() 137 } 138 139 Context("registering sent packets", func() { 140 It("accepts two consecutive packets", func() { 141 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1, EncryptionLevel: protocol.EncryptionHandshake})) 142 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2, EncryptionLevel: protocol.EncryptionHandshake})) 143 Expect(handler.handshakePackets.largestSent).To(Equal(protocol.PacketNumber(2))) 144 expectInPacketHistory([]protocol.PacketNumber{1, 2}, protocol.EncryptionHandshake) 145 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(2))) 146 }) 147 148 It("uses the same packet number space for 0-RTT and 1-RTT packets", func() { 149 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1, EncryptionLevel: protocol.Encryption0RTT})) 150 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2, EncryptionLevel: protocol.Encryption1RTT})) 151 Expect(handler.appDataPackets.largestSent).To(Equal(protocol.PacketNumber(2))) 152 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(2))) 153 }) 154 155 It("accepts packet number 0", func() { 156 sentPacket(ackElicitingPacket(&packet{PacketNumber: 0, EncryptionLevel: protocol.Encryption1RTT})) 157 Expect(handler.appDataPackets.largestSent).To(BeZero()) 158 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1, EncryptionLevel: protocol.Encryption1RTT})) 159 Expect(handler.appDataPackets.largestSent).To(Equal(protocol.PacketNumber(1))) 160 expectInPacketHistory([]protocol.PacketNumber{0, 1}, protocol.Encryption1RTT) 161 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(2))) 162 }) 163 164 It("stores the sent time", func() { 165 sendTime := time.Now().Add(-time.Minute) 166 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1, SendTime: sendTime})) 167 Expect(handler.appDataPackets.lastAckElicitingPacketTime).To(Equal(sendTime)) 168 }) 169 170 It("stores the sent time of Initial packets", func() { 171 sendTime := time.Now().Add(-time.Minute) 172 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1, SendTime: sendTime, EncryptionLevel: protocol.EncryptionInitial})) 173 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2, SendTime: sendTime.Add(time.Hour), EncryptionLevel: protocol.Encryption1RTT})) 174 Expect(handler.initialPackets.lastAckElicitingPacketTime).To(Equal(sendTime)) 175 }) 176 }) 177 178 Context("ACK processing", func() { 179 JustBeforeEach(func() { 180 for i := protocol.PacketNumber(0); i < 10; i++ { 181 sentPacket(ackElicitingPacket(&packet{PacketNumber: i})) 182 } 183 // Increase RTT, because the tests would be flaky otherwise 184 updateRTT(time.Hour) 185 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(10))) 186 }) 187 188 Context("ACK processing", func() { 189 It("accepts ACKs sent in packet 0", func() { 190 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 5}}} 191 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 192 Expect(err).ToNot(HaveOccurred()) 193 Expect(handler.appDataPackets.largestAcked).To(Equal(protocol.PacketNumber(5))) 194 }) 195 196 It("says if a 1-RTT packet was acknowledged", func() { 197 sentPacket(ackElicitingPacket(&packet{PacketNumber: 10, EncryptionLevel: protocol.Encryption0RTT})) 198 sentPacket(ackElicitingPacket(&packet{PacketNumber: 11, EncryptionLevel: protocol.Encryption0RTT})) 199 sentPacket(ackElicitingPacket(&packet{PacketNumber: 12, EncryptionLevel: protocol.Encryption1RTT})) 200 acked1RTT, err := handler.ReceivedAck( 201 &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 10, Largest: 11}}}, 202 protocol.Encryption1RTT, 203 time.Now(), 204 ) 205 Expect(err).ToNot(HaveOccurred()) 206 Expect(acked1RTT).To(BeFalse()) 207 acked1RTT, err = handler.ReceivedAck( 208 &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 11, Largest: 12}}}, 209 protocol.Encryption1RTT, 210 time.Now(), 211 ) 212 Expect(err).ToNot(HaveOccurred()) 213 Expect(acked1RTT).To(BeTrue()) 214 }) 215 216 It("accepts multiple ACKs sent in the same packet", func() { 217 ack1 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 3}}} 218 ack2 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 4}}} 219 _, err := handler.ReceivedAck(ack1, protocol.Encryption1RTT, time.Now()) 220 Expect(err).ToNot(HaveOccurred()) 221 Expect(handler.appDataPackets.largestAcked).To(Equal(protocol.PacketNumber(3))) 222 // this wouldn't happen in practice 223 // for testing purposes, we pretend to send a different ACK frame in a duplicated packet, to be able to verify that it actually doesn't get processed 224 _, err = handler.ReceivedAck(ack2, protocol.Encryption1RTT, time.Now()) 225 Expect(err).ToNot(HaveOccurred()) 226 Expect(handler.appDataPackets.largestAcked).To(Equal(protocol.PacketNumber(4))) 227 }) 228 229 It("rejects ACKs that acknowledge a skipped packet number", func() { 230 sentPacket(ackElicitingPacket(&packet{PacketNumber: 10})) 231 handler.appDataPackets.history.SkippedPacket(11) 232 sentPacket(ackElicitingPacket(&packet{PacketNumber: 12})) 233 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 10, Largest: 12}}} 234 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 235 Expect(err).To(MatchError(&qerr.TransportError{ 236 ErrorCode: qerr.ProtocolViolation, 237 ErrorMessage: "received an ACK for skipped packet number: 11 (1-RTT)", 238 })) 239 }) 240 241 It("rejects ACKs with a too high LargestAcked packet number", func() { 242 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 9999}}} 243 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 244 Expect(err).To(MatchError(&qerr.TransportError{ 245 ErrorCode: qerr.ProtocolViolation, 246 ErrorMessage: "received ACK for an unsent packet", 247 })) 248 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(10))) 249 }) 250 251 It("ignores repeated ACKs", func() { 252 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 3}}} 253 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 254 Expect(err).ToNot(HaveOccurred()) 255 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(6))) 256 _, err = handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 257 Expect(err).ToNot(HaveOccurred()) 258 Expect(handler.appDataPackets.largestAcked).To(Equal(protocol.PacketNumber(3))) 259 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(6))) 260 }) 261 }) 262 263 Context("acks the right packets", func() { 264 expectInPacketHistoryOrLost := func(expected []protocol.PacketNumber, encLevel protocol.EncryptionLevel) { 265 pnSpace := handler.getPacketNumberSpace(encLevel) 266 var length int 267 pnSpace.history.Iterate(func(p *packet) (bool, error) { 268 if !p.declaredLost { 269 length++ 270 } 271 return true, nil 272 }) 273 ExpectWithOffset(1, length+len(lostPackets)).To(Equal(len(expected))) 274 expectedLoop: 275 for _, p := range expected { 276 if getPacket(p, encLevel) != nil { 277 continue 278 } 279 for _, lostP := range lostPackets { 280 if lostP == p { 281 continue expectedLoop 282 } 283 } 284 Fail(fmt.Sprintf("Packet %d not in packet history.", p)) 285 } 286 } 287 288 It("adjusts the LargestAcked, and adjusts the bytes in flight", func() { 289 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 5}}} 290 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 291 Expect(err).ToNot(HaveOccurred()) 292 Expect(handler.appDataPackets.largestAcked).To(Equal(protocol.PacketNumber(5))) 293 expectInPacketHistoryOrLost([]protocol.PacketNumber{6, 7, 8, 9}, protocol.Encryption1RTT) 294 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(4))) 295 }) 296 297 It("acks packet 0", func() { 298 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 0}}} 299 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 300 Expect(err).ToNot(HaveOccurred()) 301 Expect(getPacket(0, protocol.Encryption1RTT)).To(BeNil()) 302 expectInPacketHistoryOrLost([]protocol.PacketNumber{1, 2, 3, 4, 5, 6, 7, 8, 9}, protocol.Encryption1RTT) 303 }) 304 305 It("calls the OnAcked callback", func() { 306 var acked bool 307 ping := &wire.PingFrame{} 308 sentPacket(ackElicitingPacket(&packet{ 309 PacketNumber: 10, 310 Frames: []Frame{{ 311 Frame: ping, 312 Handler: &customFrameHandler{ 313 onAcked: func(f wire.Frame) { 314 Expect(f).To(Equal(ping)) 315 acked = true 316 }, 317 }, 318 }}, 319 })) 320 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 10, Largest: 10}}} 321 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 322 Expect(err).ToNot(HaveOccurred()) 323 Expect(acked).To(BeTrue()) 324 }) 325 326 It("handles an ACK frame with one missing packet range", func() { 327 ack := &wire.AckFrame{ // lose 4 and 5 328 AckRanges: []wire.AckRange{ 329 {Smallest: 6, Largest: 9}, 330 {Smallest: 1, Largest: 3}, 331 }, 332 } 333 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 334 Expect(err).ToNot(HaveOccurred()) 335 expectInPacketHistoryOrLost([]protocol.PacketNumber{0, 4, 5}, protocol.Encryption1RTT) 336 }) 337 338 It("does not ack packets below the LowestAcked", func() { 339 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 3, Largest: 8}}} 340 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 341 Expect(err).ToNot(HaveOccurred()) 342 expectInPacketHistoryOrLost([]protocol.PacketNumber{0, 1, 2, 9}, protocol.Encryption1RTT) 343 }) 344 345 It("handles an ACK with multiple missing packet ranges", func() { 346 ack := &wire.AckFrame{ // packets 2, 4 and 5, and 8 were lost 347 AckRanges: []wire.AckRange{ 348 {Smallest: 9, Largest: 9}, 349 {Smallest: 6, Largest: 7}, 350 {Smallest: 3, Largest: 3}, 351 {Smallest: 1, Largest: 1}, 352 }, 353 } 354 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 355 Expect(err).ToNot(HaveOccurred()) 356 expectInPacketHistoryOrLost([]protocol.PacketNumber{0, 2, 4, 5, 8}, protocol.Encryption1RTT) 357 }) 358 359 It("processes an ACK frame that would be sent after a late arrival of a packet", func() { 360 ack1 := &wire.AckFrame{ // 5 lost 361 AckRanges: []wire.AckRange{ 362 {Smallest: 6, Largest: 6}, 363 {Smallest: 1, Largest: 4}, 364 }, 365 } 366 _, err := handler.ReceivedAck(ack1, protocol.Encryption1RTT, time.Now()) 367 Expect(err).ToNot(HaveOccurred()) 368 expectInPacketHistoryOrLost([]protocol.PacketNumber{0, 5, 7, 8, 9}, protocol.Encryption1RTT) 369 ack2 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 6}}} // now ack 5 370 _, err = handler.ReceivedAck(ack2, protocol.Encryption1RTT, time.Now()) 371 Expect(err).ToNot(HaveOccurred()) 372 expectInPacketHistoryOrLost([]protocol.PacketNumber{0, 7, 8, 9}, protocol.Encryption1RTT) 373 }) 374 375 It("processes an ACK that contains old ACK ranges", func() { 376 ack1 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 6}}} 377 _, err := handler.ReceivedAck(ack1, protocol.Encryption1RTT, time.Now()) 378 Expect(err).ToNot(HaveOccurred()) 379 expectInPacketHistoryOrLost([]protocol.PacketNumber{0, 7, 8, 9}, protocol.Encryption1RTT) 380 ack2 := &wire.AckFrame{ 381 AckRanges: []wire.AckRange{ 382 {Smallest: 8, Largest: 8}, 383 {Smallest: 3, Largest: 3}, 384 {Smallest: 1, Largest: 1}, 385 }, 386 } 387 _, err = handler.ReceivedAck(ack2, protocol.Encryption1RTT, time.Now()) 388 Expect(err).ToNot(HaveOccurred()) 389 expectInPacketHistoryOrLost([]protocol.PacketNumber{0, 7, 9}, protocol.Encryption1RTT) 390 }) 391 }) 392 393 Context("calculating RTT", func() { 394 It("computes the RTT", func() { 395 now := time.Now() 396 // First, fake the sent times of the first, second and last packet 397 getPacket(1, protocol.Encryption1RTT).SendTime = now.Add(-10 * time.Minute) 398 getPacket(2, protocol.Encryption1RTT).SendTime = now.Add(-5 * time.Minute) 399 getPacket(6, protocol.Encryption1RTT).SendTime = now.Add(-1 * time.Minute) 400 // Now, check that the proper times are used when calculating the deltas 401 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}} 402 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 403 Expect(err).ToNot(HaveOccurred()) 404 Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 10*time.Minute, 1*time.Second)) 405 ack = &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 2}}} 406 _, err = handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 407 Expect(err).ToNot(HaveOccurred()) 408 Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 5*time.Minute, 1*time.Second)) 409 ack = &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 6}}} 410 _, err = handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 411 Expect(err).ToNot(HaveOccurred()) 412 Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 1*time.Minute, 1*time.Second)) 413 }) 414 415 It("ignores the DelayTime for Initial and Handshake packets", func() { 416 sentPacket(initialPacket(&packet{PacketNumber: 1})) 417 handler.rttStats.SetMaxAckDelay(time.Hour) 418 // make sure the rttStats have a min RTT, so that the delay is used 419 handler.rttStats.UpdateRTT(5*time.Minute, 0, time.Now()) 420 getPacket(1, protocol.EncryptionInitial).SendTime = time.Now().Add(-10 * time.Minute) 421 ack := &wire.AckFrame{ 422 AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}, 423 DelayTime: 5 * time.Minute, 424 } 425 _, err := handler.ReceivedAck(ack, protocol.EncryptionInitial, time.Now()) 426 Expect(err).ToNot(HaveOccurred()) 427 Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 10*time.Minute, 1*time.Second)) 428 }) 429 430 It("uses the DelayTime in the ACK frame", func() { 431 handler.rttStats.SetMaxAckDelay(time.Hour) 432 // make sure the rttStats have a min RTT, so that the delay is used 433 handler.rttStats.UpdateRTT(5*time.Minute, 0, time.Now()) 434 getPacket(1, protocol.Encryption1RTT).SendTime = time.Now().Add(-10 * time.Minute) 435 ack := &wire.AckFrame{ 436 AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}, 437 DelayTime: 5 * time.Minute, 438 } 439 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 440 Expect(err).ToNot(HaveOccurred()) 441 Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 5*time.Minute, 1*time.Second)) 442 }) 443 444 It("limits the DelayTime in the ACK frame to max_ack_delay", func() { 445 handler.rttStats.SetMaxAckDelay(time.Minute) 446 // make sure the rttStats have a min RTT, so that the delay is used 447 handler.rttStats.UpdateRTT(5*time.Minute, 0, time.Now()) 448 getPacket(1, protocol.Encryption1RTT).SendTime = time.Now().Add(-10 * time.Minute) 449 ack := &wire.AckFrame{ 450 AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}, 451 DelayTime: 5 * time.Minute, 452 } 453 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 454 Expect(err).ToNot(HaveOccurred()) 455 Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 9*time.Minute, 1*time.Second)) 456 }) 457 }) 458 459 Context("determining which ACKs we have received an ACK for", func() { 460 JustBeforeEach(func() { 461 morePackets := []*packet{ 462 { 463 PacketNumber: 10, 464 LargestAcked: 100, 465 Frames: []Frame{{Frame: &streamFrame}}, 466 Length: 1, 467 EncryptionLevel: protocol.Encryption1RTT, 468 }, 469 { 470 PacketNumber: 11, 471 LargestAcked: 200, 472 Frames: []Frame{{Frame: &streamFrame}}, 473 Length: 1, 474 EncryptionLevel: protocol.Encryption1RTT, 475 }, 476 { 477 PacketNumber: 12, 478 Frames: []Frame{{Frame: &streamFrame}}, 479 Length: 1, 480 EncryptionLevel: protocol.Encryption1RTT, 481 }, 482 } 483 for _, packet := range morePackets { 484 sentPacket(packet) 485 } 486 }) 487 488 It("determines which ACK we have received an ACK for", func() { 489 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 10, Largest: 12}}} 490 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 491 Expect(err).ToNot(HaveOccurred()) 492 Expect(handler.GetLowestPacketNotConfirmedAcked()).To(Equal(protocol.PacketNumber(201))) 493 }) 494 495 It("doesn't do anything when the acked packet didn't contain an ACK", func() { 496 ack1 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 10, Largest: 10}}} 497 ack2 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 12, Largest: 12}}} 498 _, err := handler.ReceivedAck(ack1, protocol.Encryption1RTT, time.Now()) 499 Expect(err).ToNot(HaveOccurred()) 500 Expect(handler.GetLowestPacketNotConfirmedAcked()).To(Equal(protocol.PacketNumber(101))) 501 _, err = handler.ReceivedAck(ack2, protocol.Encryption1RTT, time.Now()) 502 Expect(err).ToNot(HaveOccurred()) 503 Expect(handler.GetLowestPacketNotConfirmedAcked()).To(Equal(protocol.PacketNumber(101))) 504 }) 505 506 It("doesn't decrease the value", func() { 507 ack1 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 11, Largest: 11}}} 508 ack2 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 10, Largest: 10}}} 509 _, err := handler.ReceivedAck(ack1, protocol.Encryption1RTT, time.Now()) 510 Expect(err).ToNot(HaveOccurred()) 511 Expect(handler.GetLowestPacketNotConfirmedAcked()).To(Equal(protocol.PacketNumber(201))) 512 _, err = handler.ReceivedAck(ack2, protocol.Encryption1RTT, time.Now()) 513 Expect(err).ToNot(HaveOccurred()) 514 Expect(handler.GetLowestPacketNotConfirmedAcked()).To(Equal(protocol.PacketNumber(201))) 515 }) 516 }) 517 }) 518 519 Context("congestion", func() { 520 var cong *mocks.MockSendAlgorithmWithDebugInfos 521 522 JustBeforeEach(func() { 523 cong = mocks.NewMockSendAlgorithmWithDebugInfos(mockCtrl) 524 handler.congestion = cong 525 }) 526 527 It("should call OnSent", func() { 528 cong.EXPECT().OnPacketSent( 529 gomock.Any(), 530 protocol.ByteCount(42), 531 protocol.PacketNumber(1), 532 protocol.ByteCount(42), 533 true, 534 ) 535 sentPacket(&packet{ 536 PacketNumber: 1, 537 Length: 42, 538 Frames: []Frame{{Frame: &wire.PingFrame{}}}, 539 EncryptionLevel: protocol.Encryption1RTT, 540 }) 541 }) 542 543 It("should call MaybeExitSlowStart and OnPacketAcked", func() { 544 rcvTime := time.Now().Add(-5 * time.Second) 545 cong.EXPECT().OnPacketSent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(3) 546 gomock.InOrder( 547 cong.EXPECT().MaybeExitSlowStart(), // must be called before packets are acked 548 cong.EXPECT().OnPacketAcked(protocol.PacketNumber(1), protocol.ByteCount(1), protocol.ByteCount(3), rcvTime), 549 cong.EXPECT().OnPacketAcked(protocol.PacketNumber(2), protocol.ByteCount(1), protocol.ByteCount(3), rcvTime), 550 ) 551 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1})) 552 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2})) 553 sentPacket(ackElicitingPacket(&packet{PacketNumber: 3})) 554 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 2}}} 555 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, rcvTime) 556 Expect(err).ToNot(HaveOccurred()) 557 }) 558 559 It("doesn't call OnPacketAcked when a retransmitted packet is acked", func() { 560 cong.EXPECT().OnPacketSent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2) 561 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1, SendTime: time.Now().Add(-time.Hour)})) 562 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2})) 563 // lose packet 1 564 gomock.InOrder( 565 cong.EXPECT().MaybeExitSlowStart(), 566 cong.EXPECT().OnCongestionEvent(protocol.PacketNumber(1), protocol.ByteCount(1), protocol.ByteCount(2)), 567 cong.EXPECT().OnPacketAcked(protocol.PacketNumber(2), protocol.ByteCount(1), protocol.ByteCount(2), gomock.Any()), 568 ) 569 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}} 570 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 571 Expect(err).ToNot(HaveOccurred()) 572 // don't EXPECT any further calls to the congestion controller 573 ack = &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 2}}} 574 _, err = handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 575 Expect(err).ToNot(HaveOccurred()) 576 }) 577 578 It("doesn't call OnCongestionEvent when a Path MTU probe packet is lost", func() { 579 cong.EXPECT().OnPacketSent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2) 580 var mtuPacketDeclaredLost bool 581 sentPacket(ackElicitingPacket(&packet{ 582 PacketNumber: 1, 583 SendTime: time.Now().Add(-time.Hour), 584 IsPathMTUProbePacket: true, 585 Frames: []Frame{ 586 { 587 Frame: &wire.PingFrame{}, 588 Handler: &customFrameHandler{onLost: func(wire.Frame) { mtuPacketDeclaredLost = true }}, 589 }, 590 }, 591 })) 592 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2})) 593 // lose packet 1, but don't EXPECT any calls to OnCongestionEvent() 594 gomock.InOrder( 595 cong.EXPECT().MaybeExitSlowStart(), 596 cong.EXPECT().OnPacketAcked(protocol.PacketNumber(2), protocol.ByteCount(1), protocol.ByteCount(2), gomock.Any()), 597 ) 598 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}} 599 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 600 Expect(err).ToNot(HaveOccurred()) 601 Expect(mtuPacketDeclaredLost).To(BeTrue()) 602 Expect(handler.bytesInFlight).To(BeZero()) 603 }) 604 605 It("calls OnPacketAcked and OnCongestionEvent with the right bytes_in_flight value", func() { 606 cong.EXPECT().OnPacketSent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(4) 607 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1, SendTime: time.Now().Add(-time.Hour)})) 608 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2, SendTime: time.Now().Add(-30 * time.Minute)})) 609 sentPacket(ackElicitingPacket(&packet{PacketNumber: 3, SendTime: time.Now().Add(-30 * time.Minute)})) 610 sentPacket(ackElicitingPacket(&packet{PacketNumber: 4, SendTime: time.Now()})) 611 // receive the first ACK 612 gomock.InOrder( 613 cong.EXPECT().MaybeExitSlowStart(), 614 cong.EXPECT().OnCongestionEvent(protocol.PacketNumber(1), protocol.ByteCount(1), protocol.ByteCount(4)), 615 cong.EXPECT().OnPacketAcked(protocol.PacketNumber(2), protocol.ByteCount(1), protocol.ByteCount(4), gomock.Any()), 616 ) 617 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}} 618 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now().Add(-30*time.Minute)) 619 Expect(err).ToNot(HaveOccurred()) 620 // receive the second ACK 621 gomock.InOrder( 622 cong.EXPECT().MaybeExitSlowStart(), 623 cong.EXPECT().OnCongestionEvent(protocol.PacketNumber(3), protocol.ByteCount(1), protocol.ByteCount(2)), 624 cong.EXPECT().OnPacketAcked(protocol.PacketNumber(4), protocol.ByteCount(1), protocol.ByteCount(2), gomock.Any()), 625 ) 626 ack = &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 4, Largest: 4}}} 627 _, err = handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 628 Expect(err).ToNot(HaveOccurred()) 629 }) 630 631 It("passes the bytes in flight to the congestion controller", func() { 632 handler.ReceivedPacket(protocol.EncryptionHandshake) 633 cong.EXPECT().OnPacketSent(gomock.Any(), protocol.ByteCount(42), gomock.Any(), protocol.ByteCount(42), true) 634 sentPacket(&packet{ 635 Length: 42, 636 EncryptionLevel: protocol.EncryptionInitial, 637 Frames: []Frame{{Frame: &wire.PingFrame{}}}, 638 SendTime: time.Now(), 639 }) 640 cong.EXPECT().CanSend(protocol.ByteCount(42)).Return(true) 641 cong.EXPECT().HasPacingBudget(gomock.Any()).Return(true) 642 handler.SendMode(time.Now()) 643 }) 644 645 It("allows sending of ACKs when congestion limited", func() { 646 handler.ReceivedPacket(protocol.EncryptionHandshake) 647 cong.EXPECT().CanSend(gomock.Any()).Return(true) 648 cong.EXPECT().HasPacingBudget(gomock.Any()).Return(true) 649 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 650 cong.EXPECT().CanSend(gomock.Any()).Return(false) 651 Expect(handler.SendMode(time.Now())).To(Equal(SendAck)) 652 }) 653 654 It("allows sending of ACKs when we're keeping track of MaxOutstandingSentPackets packets", func() { 655 handler.ReceivedPacket(protocol.EncryptionHandshake) 656 cong.EXPECT().CanSend(gomock.Any()).Return(true).AnyTimes() 657 cong.EXPECT().HasPacingBudget(gomock.Any()).Return(true).AnyTimes() 658 cong.EXPECT().OnPacketSent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() 659 for i := protocol.PacketNumber(0); i < protocol.MaxOutstandingSentPackets; i++ { 660 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 661 sentPacket(ackElicitingPacket(&packet{PacketNumber: i})) 662 } 663 Expect(handler.SendMode(time.Now())).To(Equal(SendAck)) 664 }) 665 666 It("allows PTOs, even when congestion limited", func() { 667 handler.ReceivedPacket(protocol.EncryptionHandshake) 668 // note that we don't EXPECT a call to GetCongestionWindow 669 // that means retransmissions are sent without considering the congestion window 670 handler.numProbesToSend = 1 671 handler.ptoMode = SendPTOHandshake 672 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOHandshake)) 673 }) 674 675 It("returns the pacing delay", func() { 676 t := time.Now() 677 cong.EXPECT().TimeUntilSend(gomock.Any()).Return(t) 678 Expect(handler.TimeUntilSend()).To(Equal(t)) 679 }) 680 }) 681 682 It("doesn't set an alarm if there are no outstanding packets", func() { 683 handler.ReceivedPacket(protocol.EncryptionHandshake) 684 sentPacket(ackElicitingPacket(&packet{PacketNumber: 10})) 685 sentPacket(ackElicitingPacket(&packet{PacketNumber: 11})) 686 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 10, Largest: 11}}} 687 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 688 Expect(err).ToNot(HaveOccurred()) 689 Expect(handler.GetLossDetectionTimeout()).To(BeZero()) 690 }) 691 692 It("does nothing on OnAlarm if there are no outstanding packets", func() { 693 handler.ReceivedPacket(protocol.EncryptionHandshake) 694 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 695 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 696 }) 697 698 Context("probe packets", func() { 699 It("queues a probe packet", func() { 700 sentPacket(ackElicitingPacket(&packet{PacketNumber: 10})) 701 sentPacket(ackElicitingPacket(&packet{PacketNumber: 11})) 702 queued := handler.QueueProbePacket(protocol.Encryption1RTT) 703 Expect(queued).To(BeTrue()) 704 Expect(lostPackets).To(Equal([]protocol.PacketNumber{10})) 705 }) 706 707 It("says when it can't queue a probe packet", func() { 708 queued := handler.QueueProbePacket(protocol.Encryption1RTT) 709 Expect(queued).To(BeFalse()) 710 }) 711 712 It("implements exponential backoff", func() { 713 handler.peerAddressValidated = true 714 setHandshakeConfirmed() 715 sendTime := time.Now().Add(-time.Hour) 716 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1, SendTime: sendTime})) 717 timeout := handler.GetLossDetectionTimeout().Sub(sendTime) 718 Expect(handler.GetLossDetectionTimeout().Sub(sendTime)).To(Equal(timeout)) 719 handler.ptoCount = 1 720 handler.setLossDetectionTimer() 721 Expect(handler.GetLossDetectionTimeout().Sub(sendTime)).To(Equal(2 * timeout)) 722 handler.ptoCount = 2 723 handler.setLossDetectionTimer() 724 Expect(handler.GetLossDetectionTimeout().Sub(sendTime)).To(Equal(4 * timeout)) 725 // truncated when the exponential gets too large 726 handler.ptoCount = 20 727 handler.setLossDetectionTimer() 728 Expect(handler.GetLossDetectionTimeout().Sub(sendTime)).To(Equal(maxPTODuration)) 729 // protected from rollover 730 handler.ptoCount = 100 731 handler.setLossDetectionTimer() 732 Expect(handler.GetLossDetectionTimeout().Sub(sendTime)).To(Equal(maxPTODuration)) 733 }) 734 735 It("reset the PTO count when receiving an ACK", func() { 736 handler.ReceivedPacket(protocol.EncryptionHandshake) 737 now := time.Now() 738 setHandshakeConfirmed() 739 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1, SendTime: now.Add(-time.Minute)})) 740 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2, SendTime: now.Add(-time.Minute)})) 741 handler.appDataPackets.pns.(*skippingPacketNumberGenerator).next = 3 742 Expect(handler.GetLossDetectionTimeout()).To(BeTemporally("~", now.Add(-time.Minute), time.Second)) 743 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 744 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 745 Expect(handler.ptoCount).To(BeEquivalentTo(1)) 746 _, err := handler.ReceivedAck(&wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}, protocol.Encryption1RTT, time.Now()) 747 Expect(err).ToNot(HaveOccurred()) 748 Expect(handler.ptoCount).To(BeZero()) 749 }) 750 751 It("resets the PTO mode and PTO count when a packet number space is dropped", func() { 752 handler.ReceivedPacket(protocol.EncryptionHandshake) 753 754 now := time.Now() 755 handler.rttStats.UpdateRTT(time.Second/2, 0, now) 756 Expect(handler.rttStats.SmoothedRTT()).To(Equal(time.Second / 2)) 757 Expect(handler.rttStats.PTO(true)).To(And( 758 BeNumerically(">", time.Second), 759 BeNumerically("<", 2*time.Second), 760 )) 761 sendTimeHandshake := now.Add(-2 * time.Minute) 762 sendTimeAppData := now.Add(-time.Minute) 763 764 sentPacket(ackElicitingPacket(&packet{ 765 PacketNumber: 1, 766 EncryptionLevel: protocol.EncryptionHandshake, 767 SendTime: sendTimeHandshake, 768 })) 769 sentPacket(ackElicitingPacket(&packet{ 770 PacketNumber: 2, 771 SendTime: sendTimeAppData, 772 })) 773 774 // PTO timer based on the Handshake packet 775 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 776 Expect(handler.ptoCount).To(BeEquivalentTo(1)) 777 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOHandshake)) 778 Expect(handler.GetLossDetectionTimeout()).To(Equal(sendTimeHandshake.Add(handler.rttStats.PTO(false) << 1))) 779 setHandshakeConfirmed() 780 handler.DropPackets(protocol.EncryptionHandshake) 781 // PTO timer based on the 1-RTT packet 782 Expect(handler.GetLossDetectionTimeout()).To(Equal(sendTimeAppData.Add(handler.rttStats.PTO(true)))) // no backoff. PTO count = 0 783 Expect(handler.SendMode(time.Now())).ToNot(Equal(SendPTOHandshake)) 784 Expect(handler.ptoCount).To(BeZero()) 785 }) 786 787 It("allows two 1-RTT PTOs", func() { 788 handler.ReceivedPacket(protocol.EncryptionHandshake) 789 setHandshakeConfirmed() 790 var lostPackets []protocol.PacketNumber 791 sentPacket(ackElicitingPacket(&packet{ 792 PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT), 793 SendTime: time.Now().Add(-time.Hour), 794 Frames: []Frame{ 795 { 796 Frame: &wire.PingFrame{}, 797 Handler: &customFrameHandler{onLost: func(wire.Frame) { lostPackets = append(lostPackets, 1) }}, 798 }, 799 }, 800 })) 801 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 802 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 803 sentPacket(ackElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 804 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 805 sentPacket(ackElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 806 Expect(handler.SendMode(time.Now())).ToNot(Equal(SendPTOAppData)) 807 }) 808 809 It("only counts ack-eliciting packets as probe packets", func() { 810 handler.ReceivedPacket(protocol.EncryptionHandshake) 811 setHandshakeConfirmed() 812 sentPacket(ackElicitingPacket(&packet{ 813 PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT), 814 SendTime: time.Now().Add(-time.Hour), 815 })) 816 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 817 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 818 sentPacket(ackElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 819 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 820 for i := 0; i < 30; i++ { 821 sentPacket(nonAckElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 822 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 823 } 824 sentPacket(ackElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 825 Expect(handler.SendMode(time.Now())).ToNot(Equal(SendPTOAppData)) 826 }) 827 828 It("gets two probe packets if PTO expires", func() { 829 handler.ReceivedPacket(protocol.EncryptionHandshake) 830 setHandshakeConfirmed() 831 sentPacket(ackElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 832 sentPacket(ackElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 833 834 updateRTT(time.Hour) 835 Expect(handler.appDataPackets.lossTime.IsZero()).To(BeTrue()) 836 837 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) // TLP 838 Expect(handler.ptoCount).To(BeEquivalentTo(1)) 839 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 840 sentPacket(ackElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 841 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 842 sentPacket(ackElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 843 844 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) // PTO 845 Expect(handler.ptoCount).To(BeEquivalentTo(2)) 846 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 847 sentPacket(ackElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 848 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 849 sentPacket(ackElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 850 851 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 852 }) 853 854 It("gets two probe packets if PTO expires, for Handshake packets", func() { 855 handler.ReceivedPacket(protocol.EncryptionHandshake) 856 sentPacket(initialPacket(&packet{PacketNumber: 1})) 857 sentPacket(initialPacket(&packet{PacketNumber: 2})) 858 859 updateRTT(time.Hour) 860 Expect(handler.initialPackets.lossTime.IsZero()).To(BeTrue()) 861 862 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 863 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOInitial)) 864 sentPacket(initialPacket(&packet{PacketNumber: 3})) 865 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOInitial)) 866 sentPacket(initialPacket(&packet{PacketNumber: 4})) 867 868 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 869 }) 870 871 It("doesn't send 1-RTT probe packets before the handshake completes", func() { 872 handler.ReceivedPacket(protocol.EncryptionHandshake) 873 sentPacket(ackElicitingPacket(&packet{PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT)})) 874 updateRTT(time.Hour) 875 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 876 Expect(handler.GetLossDetectionTimeout()).To(BeZero()) 877 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 878 setHandshakeConfirmed() 879 Expect(handler.GetLossDetectionTimeout()).ToNot(BeZero()) 880 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 881 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 882 }) 883 884 It("resets the send mode when it receives an acknowledgement after queueing probe packets", func() { 885 handler.ReceivedPacket(protocol.EncryptionHandshake) 886 setHandshakeConfirmed() 887 pn := handler.PopPacketNumber(protocol.Encryption1RTT) 888 sentPacket(ackElicitingPacket(&packet{PacketNumber: pn, SendTime: time.Now().Add(-time.Hour)})) 889 updateRTT(time.Second) 890 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 891 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOAppData)) 892 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: pn, Largest: pn}}} 893 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, time.Now()) 894 Expect(err).ToNot(HaveOccurred()) 895 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 896 }) 897 898 It("handles ACKs for the original packet", func() { 899 handler.ReceivedPacket(protocol.EncryptionHandshake) 900 sentPacket(ackElicitingPacket(&packet{ 901 PacketNumber: handler.PopPacketNumber(protocol.Encryption1RTT), 902 SendTime: time.Now().Add(-time.Hour), 903 })) 904 updateRTT(time.Second) 905 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 906 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 907 }) 908 909 It("doesn't set the PTO timer for Path MTU probe packets", func() { 910 handler.ReceivedPacket(protocol.EncryptionHandshake) 911 setHandshakeConfirmed() 912 updateRTT(time.Second) 913 sentPacket(ackElicitingPacket(&packet{PacketNumber: 5, SendTime: time.Now(), IsPathMTUProbePacket: true})) 914 Expect(handler.GetLossDetectionTimeout()).To(BeZero()) 915 }) 916 }) 917 918 Context("amplification limit, for the server", func() { 919 It("limits the window to 3x the bytes received, to avoid amplification attacks", func() { 920 now := time.Now() 921 handler.ReceivedPacket(protocol.EncryptionInitial) // receiving an Initial packet doesn't validate the client's address 922 handler.ReceivedBytes(200) 923 sentPacket(&packet{ 924 PacketNumber: 1, 925 Length: 599, 926 EncryptionLevel: protocol.EncryptionInitial, 927 Frames: []Frame{{Frame: &wire.PingFrame{}}}, 928 SendTime: now, 929 }) 930 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 931 sentPacket(&packet{ 932 PacketNumber: 2, 933 Length: 1, 934 EncryptionLevel: protocol.EncryptionInitial, 935 Frames: []Frame{{Frame: &wire.PingFrame{}}}, 936 SendTime: now, 937 }) 938 Expect(handler.SendMode(time.Now())).To(Equal(SendNone)) 939 }) 940 941 It("cancels the loss detection timer when it is amplification limited, and resets it when becoming unblocked", func() { 942 handler.ReceivedBytes(300) 943 sentPacket(&packet{ 944 PacketNumber: 1, 945 Length: 900, 946 EncryptionLevel: protocol.EncryptionInitial, 947 Frames: []Frame{{Frame: &wire.PingFrame{}}}, 948 SendTime: time.Now(), 949 }) 950 // Amplification limited. We don't need to set a timer now. 951 Expect(handler.GetLossDetectionTimeout()).To(BeZero()) 952 // Unblock the server. Now we should fire up the timer. 953 handler.ReceivedBytes(1) 954 Expect(handler.GetLossDetectionTimeout()).ToNot(BeZero()) 955 }) 956 957 It("resets the loss detection timer when the client's address is validated", func() { 958 handler.ReceivedBytes(300) 959 sentPacket(&packet{ 960 PacketNumber: 1, 961 Length: 900, 962 EncryptionLevel: protocol.EncryptionHandshake, 963 Frames: []Frame{{Frame: &wire.PingFrame{}}}, 964 SendTime: time.Now(), 965 }) 966 // Amplification limited. We don't need to set a timer now. 967 Expect(handler.GetLossDetectionTimeout()).To(BeZero()) 968 handler.ReceivedPacket(protocol.EncryptionHandshake) 969 Expect(handler.GetLossDetectionTimeout()).ToNot(BeZero()) 970 }) 971 972 It("cancels the loss detection alarm when all Handshake packets are acknowledged", func() { 973 t := time.Now().Add(-time.Second) 974 handler.ReceivedBytes(99999) 975 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2, SendTime: t})) 976 sentPacket(handshakePacket(&packet{PacketNumber: 3, SendTime: t})) 977 sentPacket(handshakePacket(&packet{PacketNumber: 4, SendTime: t})) 978 Expect(handler.GetLossDetectionTimeout()).ToNot(BeZero()) 979 handler.ReceivedAck(&wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 3, Largest: 4}}}, protocol.EncryptionHandshake, time.Now()) 980 Expect(handler.GetLossDetectionTimeout()).To(BeZero()) 981 }) 982 }) 983 984 Context("amplification limit, for the server, with validated address", func() { 985 JustBeforeEach(func() { 986 rttStats := utils.NewRTTStats() 987 handler = newSentPacketHandler(42, protocol.InitialPacketSize, rttStats, true, false, perspective, nil, utils.DefaultLogger) 988 }) 989 990 It("do not limits the window", func() { 991 handler.ReceivedBytes(0) 992 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 993 sentPacket(&packet{ 994 PacketNumber: 1, 995 Length: 900, 996 EncryptionLevel: protocol.EncryptionInitial, 997 Frames: []Frame{{Frame: &wire.PingFrame{}}}, 998 SendTime: time.Now(), 999 }) 1000 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 1001 }) 1002 }) 1003 1004 Context("amplification limit, for the client", func() { 1005 BeforeEach(func() { 1006 perspective = protocol.PerspectiveClient 1007 }) 1008 1009 It("sends an Initial packet to unblock the server", func() { 1010 sentPacket(initialPacket(&packet{PacketNumber: 1})) 1011 _, err := handler.ReceivedAck( 1012 &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}, 1013 protocol.EncryptionInitial, 1014 time.Now(), 1015 ) 1016 Expect(err).ToNot(HaveOccurred()) 1017 // No packets are outstanding at this point. 1018 // Make sure that a probe packet is sent. 1019 Expect(handler.GetLossDetectionTimeout()).ToNot(BeZero()) 1020 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 1021 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOInitial)) 1022 1023 // send a single packet to unblock the server 1024 sentPacket(initialPacket(&packet{PacketNumber: 2})) 1025 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 1026 1027 // Now receive an ACK for a Handshake packet. 1028 // This tells the client that the server completed address validation. 1029 sentPacket(handshakePacket(&packet{PacketNumber: 1})) 1030 handler.DropPackets(protocol.EncryptionInitial) // sending a Handshake packet drops the Initial packet number space 1031 _, err = handler.ReceivedAck( 1032 &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}, 1033 protocol.EncryptionHandshake, 1034 time.Now(), 1035 ) 1036 Expect(err).ToNot(HaveOccurred()) 1037 // Make sure that no timer is set at this point. 1038 Expect(handler.GetLossDetectionTimeout()).To(BeZero()) 1039 }) 1040 1041 It("sends a Handshake packet to unblock the server, if Initial keys were already dropped", func() { 1042 sentPacket(initialPacket(&packet{PacketNumber: 1})) 1043 _, err := handler.ReceivedAck( 1044 &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}, 1045 protocol.EncryptionInitial, 1046 time.Now(), 1047 ) 1048 Expect(err).ToNot(HaveOccurred()) 1049 1050 sentPacket(handshakePacketNonAckEliciting(&packet{PacketNumber: 1})) 1051 handler.DropPackets(protocol.EncryptionInitial) // sending a Handshake packet drops the Initial packet number space 1052 Expect(handler.GetLossDetectionTimeout()).ToNot(BeZero()) 1053 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 1054 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOHandshake)) 1055 1056 // Now receive an ACK for this packet, and send another one. 1057 _, err = handler.ReceivedAck( 1058 &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}, 1059 protocol.EncryptionHandshake, 1060 time.Now(), 1061 ) 1062 Expect(err).ToNot(HaveOccurred()) 1063 sentPacket(handshakePacketNonAckEliciting(&packet{PacketNumber: 2})) 1064 Expect(handler.GetLossDetectionTimeout()).To(BeZero()) 1065 }) 1066 1067 It("doesn't send a packet to unblock the server after handshake confirmation, even if no Handshake ACK was received", func() { 1068 sentPacket(handshakePacket(&packet{PacketNumber: 1})) 1069 Expect(handler.GetLossDetectionTimeout()).ToNot(BeZero()) 1070 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 1071 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOHandshake)) 1072 // confirm the handshake 1073 handler.DropPackets(protocol.EncryptionHandshake) 1074 Expect(handler.GetLossDetectionTimeout()).To(BeZero()) 1075 }) 1076 1077 It("correctly sets the timer after the Initial packet number space has been dropped", func() { 1078 sentPacket(initialPacket(&packet{PacketNumber: 1, SendTime: time.Now().Add(-19 * time.Second)})) 1079 _, err := handler.ReceivedAck( 1080 &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}, 1081 protocol.EncryptionInitial, 1082 time.Now(), 1083 ) 1084 Expect(err).ToNot(HaveOccurred()) 1085 sentPacket(handshakePacketNonAckEliciting(&packet{PacketNumber: 1, SendTime: time.Now()})) 1086 handler.DropPackets(protocol.EncryptionInitial) // sending a Handshake packet drops the Initial packet number space 1087 1088 pto := handler.rttStats.PTO(false) 1089 Expect(pto).ToNot(BeZero()) 1090 // pto is approximately 19 * 3. Using a number > 19 above will 1091 // run into the maxPTODuration limit 1092 Expect(handler.GetLossDetectionTimeout()).To(BeTemporally("~", time.Now().Add(pto), 10*time.Millisecond)) 1093 }) 1094 1095 It("doesn't reset the PTO count when receiving an ACK", func() { 1096 now := time.Now() 1097 sentPacket(initialPacket(&packet{PacketNumber: 1, SendTime: now.Add(-time.Minute)})) 1098 sentPacket(initialPacket(&packet{PacketNumber: 2, SendTime: now.Add(-time.Minute)})) 1099 Expect(handler.GetLossDetectionTimeout()).To(BeTemporally("~", now.Add(-time.Minute), time.Second)) 1100 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 1101 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOInitial)) 1102 Expect(handler.ptoCount).To(BeEquivalentTo(1)) 1103 _, err := handler.ReceivedAck(&wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}, protocol.EncryptionInitial, time.Now()) 1104 Expect(err).ToNot(HaveOccurred()) 1105 Expect(handler.ptoCount).To(BeEquivalentTo(1)) 1106 }) 1107 }) 1108 1109 Context("Packet-based loss detection", func() { 1110 It("declares packet below the packet loss threshold as lost", func() { 1111 now := time.Now() 1112 for i := protocol.PacketNumber(1); i <= 6; i++ { 1113 sentPacket(ackElicitingPacket(&packet{PacketNumber: i})) 1114 } 1115 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 6, Largest: 6}}} 1116 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, now) 1117 Expect(err).ToNot(HaveOccurred()) 1118 expectInPacketHistory([]protocol.PacketNumber{4, 5}, protocol.Encryption1RTT) 1119 Expect(lostPackets).To(Equal([]protocol.PacketNumber{1, 2, 3})) 1120 }) 1121 }) 1122 1123 Context("Delay-based loss detection", func() { 1124 It("immediately detects old packets as lost when receiving an ACK", func() { 1125 now := time.Now() 1126 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1, SendTime: now.Add(-time.Hour)})) 1127 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2, SendTime: now.Add(-time.Second)})) 1128 Expect(handler.appDataPackets.lossTime.IsZero()).To(BeTrue()) 1129 1130 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}} 1131 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, now) 1132 Expect(err).ToNot(HaveOccurred()) 1133 // no need to set an alarm, since packet 1 was already declared lost 1134 Expect(handler.appDataPackets.lossTime.IsZero()).To(BeTrue()) 1135 Expect(handler.bytesInFlight).To(BeZero()) 1136 }) 1137 1138 It("sets the early retransmit alarm", func() { 1139 handler.ReceivedPacket(protocol.EncryptionHandshake) 1140 handler.handshakeConfirmed = true 1141 now := time.Now() 1142 sentPacket(ackElicitingPacket(&packet{PacketNumber: 1, SendTime: now.Add(-2 * time.Second)})) 1143 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2, SendTime: now.Add(-2 * time.Second)})) 1144 sentPacket(ackElicitingPacket(&packet{PacketNumber: 3, SendTime: now})) 1145 Expect(handler.appDataPackets.lossTime.IsZero()).To(BeTrue()) 1146 1147 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}} 1148 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, now.Add(-time.Second)) 1149 Expect(err).ToNot(HaveOccurred()) 1150 Expect(handler.rttStats.SmoothedRTT()).To(Equal(time.Second)) 1151 1152 // Packet 1 should be considered lost (1+1/8) RTTs after it was sent. 1153 Expect(handler.GetLossDetectionTimeout().Sub(getPacket(1, protocol.Encryption1RTT).SendTime)).To(Equal(time.Second * 9 / 8)) 1154 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 1155 1156 expectInPacketHistory([]protocol.PacketNumber{1, 3}, protocol.Encryption1RTT) 1157 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 1158 expectInPacketHistory([]protocol.PacketNumber{3}, protocol.Encryption1RTT) 1159 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 1160 }) 1161 1162 It("sets the early retransmit alarm for crypto packets", func() { 1163 handler.ReceivedBytes(1000) 1164 now := time.Now() 1165 sentPacket(initialPacket(&packet{PacketNumber: 1, SendTime: now.Add(-2 * time.Second)})) 1166 sentPacket(initialPacket(&packet{PacketNumber: 2, SendTime: now.Add(-2 * time.Second)})) 1167 sentPacket(initialPacket(&packet{PacketNumber: 3, SendTime: now})) 1168 Expect(handler.initialPackets.lossTime.IsZero()).To(BeTrue()) 1169 1170 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}} 1171 _, err := handler.ReceivedAck(ack, protocol.EncryptionInitial, now.Add(-time.Second)) 1172 Expect(err).ToNot(HaveOccurred()) 1173 Expect(handler.rttStats.SmoothedRTT()).To(Equal(time.Second)) 1174 1175 // Packet 1 should be considered lost (1+1/8) RTTs after it was sent. 1176 Expect(handler.GetLossDetectionTimeout().Sub(getPacket(1, protocol.EncryptionInitial).SendTime)).To(Equal(time.Second * 9 / 8)) 1177 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 1178 1179 expectInPacketHistory([]protocol.PacketNumber{1, 3}, protocol.EncryptionInitial) 1180 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 1181 expectInPacketHistory([]protocol.PacketNumber{3}, protocol.EncryptionInitial) 1182 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 1183 }) 1184 1185 It("sets the early retransmit alarm for Path MTU probe packets", func() { 1186 var mtuPacketDeclaredLost bool 1187 now := time.Now() 1188 sentPacket(ackElicitingPacket(&packet{ 1189 PacketNumber: 1, 1190 SendTime: now.Add(-3 * time.Second), 1191 IsPathMTUProbePacket: true, 1192 Frames: []Frame{ 1193 { 1194 Frame: &wire.PingFrame{}, 1195 Handler: &customFrameHandler{onLost: func(wire.Frame) { mtuPacketDeclaredLost = true }}, 1196 }, 1197 }, 1198 })) 1199 sentPacket(ackElicitingPacket(&packet{PacketNumber: 2, SendTime: now.Add(-3 * time.Second)})) 1200 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}} 1201 _, err := handler.ReceivedAck(ack, protocol.Encryption1RTT, now.Add(-time.Second)) 1202 Expect(err).ToNot(HaveOccurred()) 1203 Expect(mtuPacketDeclaredLost).To(BeFalse()) 1204 Expect(handler.GetLossDetectionTimeout()).ToNot(BeZero()) 1205 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 1206 Expect(mtuPacketDeclaredLost).To(BeTrue()) 1207 Expect(handler.GetLossDetectionTimeout()).To(BeZero()) 1208 }) 1209 }) 1210 1211 Context("crypto packets", func() { 1212 It("rejects an ACK that acks packets with a higher encryption level", func() { 1213 sentPacket(ackElicitingPacket(&packet{ 1214 PacketNumber: 13, 1215 EncryptionLevel: protocol.Encryption1RTT, 1216 })) 1217 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 13, Largest: 13}}} 1218 _, err := handler.ReceivedAck(ack, protocol.EncryptionHandshake, time.Now()) 1219 Expect(err).To(MatchError(&qerr.TransportError{ 1220 ErrorCode: qerr.ProtocolViolation, 1221 ErrorMessage: "received ACK for an unsent packet", 1222 })) 1223 }) 1224 1225 It("deletes Initial packets, as a server", func() { 1226 for i := protocol.PacketNumber(0); i < 6; i++ { 1227 sentPacket(ackElicitingPacket(&packet{ 1228 PacketNumber: i, 1229 EncryptionLevel: protocol.EncryptionInitial, 1230 })) 1231 } 1232 for i := protocol.PacketNumber(0); i < 10; i++ { 1233 sentPacket(ackElicitingPacket(&packet{ 1234 PacketNumber: i, 1235 EncryptionLevel: protocol.EncryptionHandshake, 1236 })) 1237 } 1238 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(16))) 1239 handler.DropPackets(protocol.EncryptionInitial) 1240 Expect(lostPackets).To(BeEmpty()) // frames must not be queued for retransmission 1241 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(10))) 1242 Expect(handler.initialPackets).To(BeNil()) 1243 Expect(handler.handshakePackets.history.Len()).ToNot(BeZero()) 1244 }) 1245 1246 It("deletes Handshake packets", func() { 1247 for i := protocol.PacketNumber(0); i < 6; i++ { 1248 sentPacket(ackElicitingPacket(&packet{ 1249 PacketNumber: i, 1250 EncryptionLevel: protocol.EncryptionHandshake, 1251 })) 1252 } 1253 for i := protocol.PacketNumber(0); i < 10; i++ { 1254 sentPacket(ackElicitingPacket(&packet{ 1255 PacketNumber: i, 1256 EncryptionLevel: protocol.Encryption1RTT, 1257 })) 1258 } 1259 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(16))) 1260 handler.DropPackets(protocol.EncryptionHandshake) 1261 Expect(lostPackets).To(BeEmpty()) // frames must not be queued for retransmission 1262 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(10))) 1263 Expect(handler.handshakePackets).To(BeNil()) 1264 }) 1265 1266 It("doesn't retransmit 0-RTT packets when 0-RTT keys are dropped", func() { 1267 for i := protocol.PacketNumber(0); i < 6; i++ { 1268 if i == 3 { 1269 handler.appDataPackets.history.SkippedPacket(3) 1270 continue 1271 } 1272 sentPacket(ackElicitingPacket(&packet{ 1273 PacketNumber: i, 1274 EncryptionLevel: protocol.Encryption0RTT, 1275 })) 1276 } 1277 for i := protocol.PacketNumber(6); i < 12; i++ { 1278 sentPacket(ackElicitingPacket(&packet{PacketNumber: i})) 1279 } 1280 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(11))) 1281 handler.DropPackets(protocol.Encryption0RTT) 1282 Expect(lostPackets).To(BeEmpty()) 1283 Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(6))) 1284 }) 1285 1286 It("cancels the PTO when dropping a packet number space", func() { 1287 handler.ReceivedPacket(protocol.EncryptionHandshake) 1288 now := time.Now() 1289 sentPacket(handshakePacket(&packet{PacketNumber: 1, SendTime: now.Add(-time.Minute)})) 1290 sentPacket(handshakePacket(&packet{PacketNumber: 2, SendTime: now.Add(-time.Minute)})) 1291 Expect(handler.GetLossDetectionTimeout()).To(BeTemporally("~", now.Add(-time.Minute), time.Second)) 1292 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 1293 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOHandshake)) 1294 Expect(handler.ptoCount).To(BeEquivalentTo(1)) 1295 handler.DropPackets(protocol.EncryptionHandshake) 1296 Expect(handler.ptoCount).To(BeZero()) 1297 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 1298 }) 1299 }) 1300 1301 Context("peeking and popping packet number", func() { 1302 It("peeks and pops the initial packet number", func() { 1303 pn, _ := handler.PeekPacketNumber(protocol.EncryptionInitial) 1304 Expect(pn).To(Equal(protocol.PacketNumber(42))) 1305 Expect(handler.PopPacketNumber(protocol.EncryptionInitial)).To(Equal(protocol.PacketNumber(42))) 1306 }) 1307 1308 It("peeks and pops beyond the initial packet number", func() { 1309 Expect(handler.PopPacketNumber(protocol.EncryptionInitial)).To(Equal(protocol.PacketNumber(42))) 1310 Expect(handler.PopPacketNumber(protocol.EncryptionInitial)).To(BeNumerically(">", 42)) 1311 }) 1312 1313 It("starts at 0 for handshake and application-data packet number space", func() { 1314 pn, _ := handler.PeekPacketNumber(protocol.EncryptionHandshake) 1315 Expect(pn).To(BeZero()) 1316 Expect(handler.PopPacketNumber(protocol.EncryptionHandshake)).To(BeZero()) 1317 pn, _ = handler.PeekPacketNumber(protocol.Encryption1RTT) 1318 Expect(pn).To(BeZero()) 1319 Expect(handler.PopPacketNumber(protocol.Encryption1RTT)).To(BeZero()) 1320 }) 1321 }) 1322 1323 Context("for the client", func() { 1324 BeforeEach(func() { 1325 perspective = protocol.PerspectiveClient 1326 }) 1327 1328 It("considers the server's address validated right away", func() { 1329 }) 1330 1331 It("queues outstanding packets for retransmission, cancels alarms and resets PTO count when receiving a Retry", func() { 1332 sentPacket(initialPacket(&packet{PacketNumber: 42})) 1333 Expect(handler.GetLossDetectionTimeout()).ToNot(BeZero()) 1334 Expect(handler.bytesInFlight).ToNot(BeZero()) 1335 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 1336 // now receive a Retry 1337 Expect(handler.ResetForRetry(time.Now())).To(Succeed()) 1338 Expect(lostPackets).To(Equal([]protocol.PacketNumber{42})) 1339 Expect(handler.bytesInFlight).To(BeZero()) 1340 Expect(handler.GetLossDetectionTimeout()).To(BeZero()) 1341 Expect(handler.SendMode(time.Now())).To(Equal(SendAny)) 1342 Expect(handler.ptoCount).To(BeZero()) 1343 }) 1344 1345 It("queues outstanding frames for retransmission and cancels alarms when receiving a Retry", func() { 1346 var lostInitial, lost0RTT bool 1347 sentPacket(&packet{ 1348 PacketNumber: 13, 1349 EncryptionLevel: protocol.EncryptionInitial, 1350 Frames: []Frame{ 1351 { 1352 Frame: &wire.CryptoFrame{Data: []byte("foobar")}, 1353 Handler: &customFrameHandler{onLost: func(wire.Frame) { lostInitial = true }}, 1354 }, 1355 }, 1356 Length: 100, 1357 }) 1358 pn := handler.PopPacketNumber(protocol.Encryption0RTT) 1359 sentPacket(&packet{ 1360 PacketNumber: pn, 1361 EncryptionLevel: protocol.Encryption0RTT, 1362 Frames: []Frame{ 1363 { 1364 Frame: &wire.StreamFrame{Data: []byte("foobar")}, 1365 Handler: &customFrameHandler{onLost: func(wire.Frame) { lost0RTT = true }}, 1366 }, 1367 }, 1368 Length: 999, 1369 }) 1370 Expect(handler.bytesInFlight).ToNot(BeZero()) 1371 // now receive a Retry 1372 Expect(handler.ResetForRetry(time.Now())).To(Succeed()) 1373 Expect(handler.bytesInFlight).To(BeZero()) 1374 Expect(lostInitial).To(BeTrue()) 1375 Expect(lost0RTT).To(BeTrue()) 1376 1377 // make sure we keep increasing the packet number for 0-RTT packets 1378 Expect(handler.PopPacketNumber(protocol.Encryption0RTT)).To(BeNumerically(">", pn)) 1379 }) 1380 1381 It("uses a Retry for an RTT estimate, if it was not retransmitted", func() { 1382 now := time.Now() 1383 sentPacket(ackElicitingPacket(&packet{ 1384 PacketNumber: 42, 1385 EncryptionLevel: protocol.EncryptionInitial, 1386 SendTime: now, 1387 })) 1388 sentPacket(ackElicitingPacket(&packet{ 1389 PacketNumber: 43, 1390 EncryptionLevel: protocol.EncryptionInitial, 1391 SendTime: now.Add(500 * time.Millisecond), 1392 })) 1393 Expect(handler.ResetForRetry(now.Add(time.Second))).To(Succeed()) 1394 Expect(handler.rttStats.SmoothedRTT()).To(Equal(time.Second)) 1395 }) 1396 1397 It("uses a Retry for an RTT estimate, but doesn't set the RTT to a value lower than 5ms", func() { 1398 now := time.Now() 1399 sentPacket(ackElicitingPacket(&packet{ 1400 PacketNumber: 42, 1401 EncryptionLevel: protocol.EncryptionInitial, 1402 SendTime: now, 1403 })) 1404 sentPacket(ackElicitingPacket(&packet{ 1405 PacketNumber: 43, 1406 EncryptionLevel: protocol.EncryptionInitial, 1407 SendTime: now.Add(2 * time.Millisecond), 1408 })) 1409 Expect(handler.ResetForRetry(now.Add(4 * time.Millisecond))).To(Succeed()) 1410 Expect(minRTTAfterRetry).To(BeNumerically(">", 4*time.Millisecond)) 1411 Expect(handler.rttStats.SmoothedRTT()).To(Equal(minRTTAfterRetry)) 1412 }) 1413 1414 It("doesn't use a Retry for an RTT estimate, if it was not retransmitted", func() { 1415 now := time.Now() 1416 sentPacket(ackElicitingPacket(&packet{ 1417 PacketNumber: 42, 1418 EncryptionLevel: protocol.EncryptionInitial, 1419 SendTime: now, 1420 })) 1421 Expect(handler.OnLossDetectionTimeout()).To(Succeed()) 1422 Expect(handler.SendMode(time.Now())).To(Equal(SendPTOInitial)) 1423 sentPacket(ackElicitingPacket(&packet{ 1424 PacketNumber: 43, 1425 EncryptionLevel: protocol.EncryptionInitial, 1426 SendTime: now.Add(500 * time.Millisecond), 1427 })) 1428 Expect(handler.ResetForRetry(now.Add(time.Second))).To(Succeed()) 1429 Expect(handler.rttStats.SmoothedRTT()).To(BeZero()) 1430 }) 1431 }) 1432 1433 Context("ECN handling", func() { 1434 var ecnHandler *MockECNHandler 1435 var cong *mocks.MockSendAlgorithmWithDebugInfos 1436 1437 JustBeforeEach(func() { 1438 cong = mocks.NewMockSendAlgorithmWithDebugInfos(mockCtrl) 1439 cong.EXPECT().OnPacketSent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() 1440 cong.EXPECT().OnPacketAcked(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() 1441 cong.EXPECT().MaybeExitSlowStart().AnyTimes() 1442 ecnHandler = NewMockECNHandler(mockCtrl) 1443 lostPackets = nil 1444 rttStats := utils.NewRTTStats() 1445 rttStats.UpdateRTT(time.Hour, 0, time.Now()) 1446 handler = newSentPacketHandler(42, protocol.InitialPacketSize, rttStats, false, false, perspective, nil, utils.DefaultLogger) 1447 handler.ecnTracker = ecnHandler 1448 handler.congestion = cong 1449 }) 1450 1451 It("informs about sent packets", func() { 1452 // Check that only 1-RTT packets are reported 1453 handler.SentPacket(time.Now(), 100, -1, nil, nil, protocol.EncryptionInitial, protocol.ECT1, 1200, false) 1454 handler.SentPacket(time.Now(), 101, -1, nil, nil, protocol.EncryptionHandshake, protocol.ECT0, 1200, false) 1455 handler.SentPacket(time.Now(), 102, -1, nil, nil, protocol.Encryption0RTT, protocol.ECNCE, 1200, false) 1456 1457 ecnHandler.EXPECT().SentPacket(protocol.PacketNumber(103), protocol.ECT1) 1458 handler.SentPacket(time.Now(), 103, -1, nil, nil, protocol.Encryption1RTT, protocol.ECT1, 1200, false) 1459 }) 1460 1461 It("informs about sent packets", func() { 1462 // Check that only 1-RTT packets are reported 1463 handler.SentPacket(time.Now(), 100, -1, nil, nil, protocol.EncryptionInitial, protocol.ECT1, 1200, false) 1464 handler.SentPacket(time.Now(), 101, -1, nil, nil, protocol.EncryptionHandshake, protocol.ECT0, 1200, false) 1465 handler.SentPacket(time.Now(), 102, -1, nil, nil, protocol.Encryption0RTT, protocol.ECNCE, 1200, false) 1466 1467 ecnHandler.EXPECT().SentPacket(protocol.PacketNumber(103), protocol.ECT1) 1468 handler.SentPacket(time.Now(), 103, -1, nil, nil, protocol.Encryption1RTT, protocol.ECT1, 1200, false) 1469 }) 1470 1471 It("informs about lost packets", func() { 1472 for i := 10; i < 20; i++ { 1473 ecnHandler.EXPECT().SentPacket(protocol.PacketNumber(i), protocol.ECT1) 1474 handler.SentPacket(time.Now(), protocol.PacketNumber(i), -1, []StreamFrame{{Frame: &streamFrame}}, nil, protocol.Encryption1RTT, protocol.ECT1, 1200, false) 1475 } 1476 cong.EXPECT().OnCongestionEvent(gomock.Any(), gomock.Any(), gomock.Any()).Times(3) 1477 ecnHandler.EXPECT().LostPacket(protocol.PacketNumber(10)) 1478 ecnHandler.EXPECT().LostPacket(protocol.PacketNumber(11)) 1479 ecnHandler.EXPECT().LostPacket(protocol.PacketNumber(12)) 1480 ecnHandler.EXPECT().HandleNewlyAcked(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()) 1481 _, err := handler.ReceivedAck(&wire.AckFrame{AckRanges: []wire.AckRange{{Largest: 16, Smallest: 13}}}, protocol.Encryption1RTT, time.Now()) 1482 Expect(err).ToNot(HaveOccurred()) 1483 }) 1484 1485 It("processes ACKs", func() { 1486 // Check that we only care about 1-RTT packets. 1487 handler.SentPacket(time.Now(), 100, -1, []StreamFrame{{Frame: &streamFrame}}, nil, protocol.EncryptionInitial, protocol.ECT1, 1200, false) 1488 _, err := handler.ReceivedAck(&wire.AckFrame{AckRanges: []wire.AckRange{{Largest: 100, Smallest: 100}}}, protocol.EncryptionInitial, time.Now()) 1489 Expect(err).ToNot(HaveOccurred()) 1490 1491 for i := 10; i < 20; i++ { 1492 ecnHandler.EXPECT().SentPacket(protocol.PacketNumber(i), protocol.ECT1) 1493 handler.SentPacket(time.Now(), protocol.PacketNumber(i), -1, []StreamFrame{{Frame: &streamFrame}}, nil, protocol.Encryption1RTT, protocol.ECT1, 1200, false) 1494 } 1495 ecnHandler.EXPECT().HandleNewlyAcked(gomock.Any(), int64(1), int64(2), int64(3)).DoAndReturn(func(packets []*packet, _, _, _ int64) bool { 1496 Expect(packets).To(HaveLen(5)) 1497 Expect(packets[0].PacketNumber).To(Equal(protocol.PacketNumber(10))) 1498 Expect(packets[1].PacketNumber).To(Equal(protocol.PacketNumber(11))) 1499 Expect(packets[2].PacketNumber).To(Equal(protocol.PacketNumber(12))) 1500 Expect(packets[3].PacketNumber).To(Equal(protocol.PacketNumber(14))) 1501 Expect(packets[4].PacketNumber).To(Equal(protocol.PacketNumber(15))) 1502 return false 1503 }) 1504 _, err = handler.ReceivedAck(&wire.AckFrame{ 1505 AckRanges: []wire.AckRange{ 1506 {Largest: 15, Smallest: 14}, 1507 {Largest: 12, Smallest: 10}, 1508 }, 1509 ECT0: 1, 1510 ECT1: 2, 1511 ECNCE: 3, 1512 }, protocol.Encryption1RTT, time.Now()) 1513 Expect(err).ToNot(HaveOccurred()) 1514 }) 1515 1516 It("ignores reordered ACKs", func() { 1517 for i := 10; i < 20; i++ { 1518 ecnHandler.EXPECT().SentPacket(protocol.PacketNumber(i), protocol.ECT1) 1519 handler.SentPacket(time.Now(), protocol.PacketNumber(i), -1, []StreamFrame{{Frame: &streamFrame}}, nil, protocol.Encryption1RTT, protocol.ECT1, 1200, false) 1520 } 1521 ecnHandler.EXPECT().HandleNewlyAcked(gomock.Any(), int64(1), int64(2), int64(3)).DoAndReturn(func(packets []*packet, _, _, _ int64) bool { 1522 Expect(packets).To(HaveLen(2)) 1523 Expect(packets[0].PacketNumber).To(Equal(protocol.PacketNumber(11))) 1524 Expect(packets[1].PacketNumber).To(Equal(protocol.PacketNumber(12))) 1525 return false 1526 }) 1527 _, err := handler.ReceivedAck(&wire.AckFrame{ 1528 AckRanges: []wire.AckRange{{Largest: 12, Smallest: 11}}, 1529 ECT0: 1, 1530 ECT1: 2, 1531 ECNCE: 3, 1532 }, protocol.Encryption1RTT, time.Now()) 1533 Expect(err).ToNot(HaveOccurred()) 1534 // acknowledge packet 10 now, but don't increase the largest acked 1535 _, err = handler.ReceivedAck(&wire.AckFrame{ 1536 AckRanges: []wire.AckRange{{Largest: 12, Smallest: 10}}, 1537 ECT0: 1, 1538 ECNCE: 3, 1539 }, protocol.Encryption1RTT, time.Now()) 1540 Expect(err).ToNot(HaveOccurred()) 1541 }) 1542 1543 It("ignores ACKs that don't increase the largest acked", func() { 1544 for i := 10; i < 20; i++ { 1545 ecnHandler.EXPECT().SentPacket(protocol.PacketNumber(i), protocol.ECT1) 1546 handler.SentPacket(time.Now(), protocol.PacketNumber(i), -1, []StreamFrame{{Frame: &streamFrame}}, nil, protocol.Encryption1RTT, protocol.ECT1, 1200, false) 1547 } 1548 ecnHandler.EXPECT().HandleNewlyAcked(gomock.Any(), int64(1), int64(2), int64(3)).DoAndReturn(func(packets []*packet, _, _, _ int64) bool { 1549 Expect(packets).To(HaveLen(1)) 1550 Expect(packets[0].PacketNumber).To(Equal(protocol.PacketNumber(11))) 1551 return false 1552 }) 1553 _, err := handler.ReceivedAck(&wire.AckFrame{ 1554 AckRanges: []wire.AckRange{{Largest: 11, Smallest: 11}}, 1555 ECT0: 1, 1556 ECT1: 2, 1557 ECNCE: 3, 1558 }, protocol.Encryption1RTT, time.Now()) 1559 Expect(err).ToNot(HaveOccurred()) 1560 _, err = handler.ReceivedAck(&wire.AckFrame{ 1561 AckRanges: []wire.AckRange{{Largest: 11, Smallest: 10}}, 1562 ECT0: 1, 1563 ECNCE: 3, 1564 }, protocol.Encryption1RTT, time.Now()) 1565 Expect(err).ToNot(HaveOccurred()) 1566 }) 1567 1568 It("informs the congestion controller about CE events", func() { 1569 for i := 10; i < 20; i++ { 1570 ecnHandler.EXPECT().SentPacket(protocol.PacketNumber(i), protocol.ECT0) 1571 handler.SentPacket(time.Now(), protocol.PacketNumber(i), -1, []StreamFrame{{Frame: &streamFrame}}, nil, protocol.Encryption1RTT, protocol.ECT0, 1200, false) 1572 } 1573 ecnHandler.EXPECT().HandleNewlyAcked(gomock.Any(), int64(0), int64(0), int64(0)).Return(true) 1574 cong.EXPECT().OnCongestionEvent(protocol.PacketNumber(15), gomock.Any(), gomock.Any()) 1575 _, err := handler.ReceivedAck(&wire.AckFrame{AckRanges: []wire.AckRange{{Largest: 15, Smallest: 10}}}, protocol.Encryption1RTT, time.Now()) 1576 Expect(err).ToNot(HaveOccurred()) 1577 }) 1578 }) 1579 })