received_packet_history_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package ackhandler
  2. import (
  3. "github.com/lucas-clemente/quic-go/internal/protocol"
  4. "github.com/lucas-clemente/quic-go/internal/utils"
  5. "github.com/lucas-clemente/quic-go/internal/wire"
  6. . "github.com/onsi/ginkgo"
  7. . "github.com/onsi/gomega"
  8. )
  9. var _ = Describe("receivedPacketHistory", func() {
  10. var (
  11. hist *receivedPacketHistory
  12. )
  13. BeforeEach(func() {
  14. hist = newReceivedPacketHistory()
  15. })
  16. Context("ranges", func() {
  17. It("adds the first packet", func() {
  18. hist.ReceivedPacket(4)
  19. Expect(hist.ranges.Len()).To(Equal(1))
  20. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4}))
  21. })
  22. It("doesn't care about duplicate packets", func() {
  23. hist.ReceivedPacket(4)
  24. Expect(hist.ranges.Len()).To(Equal(1))
  25. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4}))
  26. })
  27. It("adds a few consecutive packets", func() {
  28. hist.ReceivedPacket(4)
  29. hist.ReceivedPacket(5)
  30. hist.ReceivedPacket(6)
  31. Expect(hist.ranges.Len()).To(Equal(1))
  32. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 6}))
  33. })
  34. It("doesn't care about a duplicate packet contained in an existing range", func() {
  35. hist.ReceivedPacket(4)
  36. hist.ReceivedPacket(5)
  37. hist.ReceivedPacket(6)
  38. hist.ReceivedPacket(5)
  39. Expect(hist.ranges.Len()).To(Equal(1))
  40. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 6}))
  41. })
  42. It("extends a range at the front", func() {
  43. hist.ReceivedPacket(4)
  44. hist.ReceivedPacket(3)
  45. Expect(hist.ranges.Len()).To(Equal(1))
  46. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 3, End: 4}))
  47. })
  48. It("creates a new range when a packet is lost", func() {
  49. hist.ReceivedPacket(4)
  50. hist.ReceivedPacket(6)
  51. Expect(hist.ranges.Len()).To(Equal(2))
  52. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4}))
  53. Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 6, End: 6}))
  54. })
  55. It("creates a new range in between two ranges", func() {
  56. hist.ReceivedPacket(4)
  57. hist.ReceivedPacket(10)
  58. Expect(hist.ranges.Len()).To(Equal(2))
  59. hist.ReceivedPacket(7)
  60. Expect(hist.ranges.Len()).To(Equal(3))
  61. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4}))
  62. Expect(hist.ranges.Front().Next().Value).To(Equal(utils.PacketInterval{Start: 7, End: 7}))
  63. Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 10, End: 10}))
  64. })
  65. It("creates a new range before an existing range for a belated packet", func() {
  66. hist.ReceivedPacket(6)
  67. hist.ReceivedPacket(4)
  68. Expect(hist.ranges.Len()).To(Equal(2))
  69. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4}))
  70. Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 6, End: 6}))
  71. })
  72. It("extends a previous range at the end", func() {
  73. hist.ReceivedPacket(4)
  74. hist.ReceivedPacket(7)
  75. hist.ReceivedPacket(5)
  76. Expect(hist.ranges.Len()).To(Equal(2))
  77. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 5}))
  78. Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 7, End: 7}))
  79. })
  80. It("extends a range at the front", func() {
  81. hist.ReceivedPacket(4)
  82. hist.ReceivedPacket(7)
  83. hist.ReceivedPacket(6)
  84. Expect(hist.ranges.Len()).To(Equal(2))
  85. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4}))
  86. Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 6, End: 7}))
  87. })
  88. It("closes a range", func() {
  89. hist.ReceivedPacket(6)
  90. hist.ReceivedPacket(4)
  91. Expect(hist.ranges.Len()).To(Equal(2))
  92. hist.ReceivedPacket(5)
  93. Expect(hist.ranges.Len()).To(Equal(1))
  94. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 6}))
  95. })
  96. It("closes a range in the middle", func() {
  97. hist.ReceivedPacket(1)
  98. hist.ReceivedPacket(10)
  99. hist.ReceivedPacket(4)
  100. hist.ReceivedPacket(6)
  101. Expect(hist.ranges.Len()).To(Equal(4))
  102. hist.ReceivedPacket(5)
  103. Expect(hist.ranges.Len()).To(Equal(3))
  104. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 1, End: 1}))
  105. Expect(hist.ranges.Front().Next().Value).To(Equal(utils.PacketInterval{Start: 4, End: 6}))
  106. Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 10, End: 10}))
  107. })
  108. })
  109. Context("deleting", func() {
  110. It("does nothing when the history is empty", func() {
  111. hist.DeleteBelow(5)
  112. Expect(hist.ranges.Len()).To(BeZero())
  113. })
  114. It("deletes a range", func() {
  115. hist.ReceivedPacket(4)
  116. hist.ReceivedPacket(5)
  117. hist.ReceivedPacket(10)
  118. hist.DeleteBelow(6)
  119. Expect(hist.ranges.Len()).To(Equal(1))
  120. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 10, End: 10}))
  121. })
  122. It("deletes multiple ranges", func() {
  123. hist.ReceivedPacket(1)
  124. hist.ReceivedPacket(5)
  125. hist.ReceivedPacket(10)
  126. hist.DeleteBelow(8)
  127. Expect(hist.ranges.Len()).To(Equal(1))
  128. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 10, End: 10}))
  129. })
  130. It("adjusts a range, if packets are delete from an existing range", func() {
  131. hist.ReceivedPacket(3)
  132. hist.ReceivedPacket(4)
  133. hist.ReceivedPacket(5)
  134. hist.ReceivedPacket(6)
  135. hist.ReceivedPacket(7)
  136. hist.DeleteBelow(5)
  137. Expect(hist.ranges.Len()).To(Equal(1))
  138. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 5, End: 7}))
  139. })
  140. It("adjusts a range, if only one packet remains in the range", func() {
  141. hist.ReceivedPacket(4)
  142. hist.ReceivedPacket(5)
  143. hist.ReceivedPacket(10)
  144. hist.DeleteBelow(5)
  145. Expect(hist.ranges.Len()).To(Equal(2))
  146. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 5, End: 5}))
  147. Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 10, End: 10}))
  148. })
  149. It("keeps a one-packet range, if deleting up to the packet directly below", func() {
  150. hist.ReceivedPacket(4)
  151. hist.DeleteBelow(4)
  152. Expect(hist.ranges.Len()).To(Equal(1))
  153. Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4}))
  154. })
  155. Context("DoS protection", func() {
  156. It("doesn't create more than MaxTrackedReceivedAckRanges ranges", func() {
  157. for i := protocol.PacketNumber(1); i <= protocol.MaxTrackedReceivedAckRanges; i++ {
  158. err := hist.ReceivedPacket(2 * i)
  159. Expect(err).ToNot(HaveOccurred())
  160. }
  161. err := hist.ReceivedPacket(2*protocol.MaxTrackedReceivedAckRanges + 2)
  162. Expect(err).To(MatchError(errTooManyOutstandingReceivedAckRanges))
  163. })
  164. It("doesn't consider already deleted ranges for MaxTrackedReceivedAckRanges", func() {
  165. for i := protocol.PacketNumber(1); i <= protocol.MaxTrackedReceivedAckRanges; i++ {
  166. err := hist.ReceivedPacket(2 * i)
  167. Expect(err).ToNot(HaveOccurred())
  168. }
  169. err := hist.ReceivedPacket(2*protocol.MaxTrackedReceivedAckRanges + 2)
  170. Expect(err).To(MatchError(errTooManyOutstandingReceivedAckRanges))
  171. hist.DeleteBelow(protocol.MaxTrackedReceivedAckRanges) // deletes about half of the ranges
  172. err = hist.ReceivedPacket(2*protocol.MaxTrackedReceivedAckRanges + 4)
  173. Expect(err).ToNot(HaveOccurred())
  174. })
  175. })
  176. })
  177. Context("ACK range export", func() {
  178. It("returns nil if there are no ranges", func() {
  179. Expect(hist.GetAckRanges()).To(BeNil())
  180. })
  181. It("gets a single ACK range", func() {
  182. hist.ReceivedPacket(4)
  183. hist.ReceivedPacket(5)
  184. ackRanges := hist.GetAckRanges()
  185. Expect(ackRanges).To(HaveLen(1))
  186. Expect(ackRanges[0]).To(Equal(wire.AckRange{Smallest: 4, Largest: 5}))
  187. })
  188. It("gets multiple ACK ranges", func() {
  189. hist.ReceivedPacket(4)
  190. hist.ReceivedPacket(5)
  191. hist.ReceivedPacket(6)
  192. hist.ReceivedPacket(1)
  193. hist.ReceivedPacket(11)
  194. hist.ReceivedPacket(10)
  195. hist.ReceivedPacket(2)
  196. ackRanges := hist.GetAckRanges()
  197. Expect(ackRanges).To(HaveLen(3))
  198. Expect(ackRanges[0]).To(Equal(wire.AckRange{Smallest: 10, Largest: 11}))
  199. Expect(ackRanges[1]).To(Equal(wire.AckRange{Smallest: 4, Largest: 6}))
  200. Expect(ackRanges[2]).To(Equal(wire.AckRange{Smallest: 1, Largest: 2}))
  201. })
  202. })
  203. Context("Getting the highest ACK range", func() {
  204. It("returns the zero value if there are no ranges", func() {
  205. Expect(hist.GetHighestAckRange()).To(BeZero())
  206. })
  207. It("gets a single ACK range", func() {
  208. hist.ReceivedPacket(4)
  209. hist.ReceivedPacket(5)
  210. Expect(hist.GetHighestAckRange()).To(Equal(wire.AckRange{Smallest: 4, Largest: 5}))
  211. })
  212. It("gets the highest of multiple ACK ranges", func() {
  213. hist.ReceivedPacket(3)
  214. hist.ReceivedPacket(6)
  215. hist.ReceivedPacket(7)
  216. Expect(hist.GetHighestAckRange()).To(Equal(wire.AckRange{Smallest: 6, Largest: 7}))
  217. })
  218. })
  219. })