1 /** @file
2  *
3  *  A brief file description
4  *
5  *  @section license License
6  *
7  *  Licensed to the Apache Software Foundation (ASF) under one
8  *  or more contributor license agreements.  See the NOTICE file
9  *  distributed with this work for additional information
10  *  regarding copyright ownership.  The ASF licenses this file
11  *  to you under the Apache License, Version 2.0 (the
12  *  "License"); you may not use this file except in compliance
13  *  with the License.  You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  */
23 
24 #include "catch.hpp"
25 
26 #include "quic/QUICAckFrameCreator.h"
27 
28 TEST_CASE("QUICAckFrameManager", "[quic]")
29 {
30   QUICAckFrameManager ack_manager;
31   QUICEncryptionLevel level = QUICEncryptionLevel::INITIAL;
32   uint8_t frame_buf[QUICFrame::MAX_INSTANCE_SIZE];
33 
34   // Initial state
35   QUICFrame *ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
36   QUICAckFrame *frame  = static_cast<QUICAckFrame *>(ack_frame);
37   CHECK(frame == nullptr);
38 
39   // One packet
40   ack_manager.update(level, 1, 1, false);
41   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
42   frame     = static_cast<QUICAckFrame *>(ack_frame);
43   CHECK(frame != nullptr);
44   CHECK(frame->ack_block_count() == 0);
45   CHECK(frame->largest_acknowledged() == 1);
46   CHECK(frame->ack_block_section()->first_ack_block() == 0);
47   ack_frame->~QUICFrame();
48 
49   // retry
50   CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
51 
52   // Not sequential
53   ack_manager.update(level, 2, 1, false);
54   ack_manager.update(level, 5, 1, false);
55   ack_manager.update(level, 3, 1, false);
56   ack_manager.update(level, 4, 1, false);
57   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
58   frame     = static_cast<QUICAckFrame *>(ack_frame);
59   CHECK(frame != nullptr);
60   CHECK(frame->ack_block_count() == 0);
61   CHECK(frame->largest_acknowledged() == 5);
62   CHECK(frame->ack_block_section()->first_ack_block() == 4);
63   ack_frame->~QUICFrame();
64 
65   // Loss
66   ack_manager.update(level, 6, 1, false);
67   ack_manager.update(level, 7, 1, false);
68   ack_manager.update(level, 10, 1, false);
69   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
70   frame     = static_cast<QUICAckFrame *>(ack_frame);
71   CHECK(frame != nullptr);
72   CHECK(frame->ack_block_count() == 1);
73   CHECK(frame->largest_acknowledged() == 10);
74   CHECK(frame->ack_block_section()->first_ack_block() == 0);
75   CHECK(frame->ack_block_section()->begin()->gap() == 1);
76   ack_frame->~QUICFrame();
77 
78   // on frame acked
79   ack_manager.on_frame_acked(frame->id());
80 
81   CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
82   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
83   CHECK(ack_frame == nullptr);
84 
85   ack_manager.update(level, 11, 1, false);
86   ack_manager.update(level, 12, 1, false);
87   ack_manager.update(level, 13, 1, false);
88   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
89   frame     = static_cast<QUICAckFrame *>(ack_frame);
90   CHECK(frame != nullptr);
91   CHECK(frame->ack_block_count() == 0);
92   CHECK(frame->largest_acknowledged() == 13);
93   CHECK(frame->ack_block_section()->first_ack_block() == 2);
94   CHECK(frame->ack_block_section()->begin()->gap() == 0);
95   ack_frame->~QUICFrame();
96 
97   ack_manager.on_frame_acked(frame->id());
98 
99   // ack-only
100   ack_manager.update(level, 14, 1, true);
101   ack_manager.update(level, 15, 1, true);
102   ack_manager.update(level, 16, 1, true);
103   CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
104   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
105 
106   ack_manager.update(level, 17, 1, false);
107   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
108   frame     = static_cast<QUICAckFrame *>(ack_frame);
109   CHECK(frame != nullptr);
110   CHECK(frame->ack_block_count() == 0);
111   CHECK(frame->largest_acknowledged() == 17);
112   CHECK(frame->ack_block_section()->first_ack_block() == 3);
113   CHECK(frame->ack_block_section()->begin()->gap() == 0);
114   ack_frame->~QUICFrame();
115 }
116 
117 TEST_CASE("QUICAckFrameManager should send", "[quic]")
118 {
119   SECTION("QUIC unorder packet", "[quic]")
120   {
121     QUICAckFrameManager ack_manager;
122 
123     QUICEncryptionLevel level = QUICEncryptionLevel::ONE_RTT;
124     ack_manager.update(level, 2, 1, false);
125     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
126   }
127 
128   SECTION("QUIC delay ack and unorder packet", "[quic]")
129   {
130     QUICAckFrameManager ack_manager;
131 
132     QUICEncryptionLevel level = QUICEncryptionLevel::ONE_RTT;
133     ack_manager.update(level, 0, 1, false);
134     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
135 
136     ack_manager.update(level, 1, 1, false);
137     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
138 
139     ack_manager.update(level, 3, 1, false);
140     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
141   }
142 
143   SECTION("QUIC delay too much time", "[quic]")
144   {
145     Thread::get_hrtime_updated();
146     QUICAckFrameManager ack_manager;
147 
148     QUICEncryptionLevel level = QUICEncryptionLevel::ONE_RTT;
149     ack_manager.update(level, 0, 1, false);
150     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
151 
152     sleep(1);
153     Thread::get_hrtime_updated();
154     ack_manager.update(level, 1, 1, false);
155     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
156   }
157 
158   SECTION("QUIC intial packet", "[quic]")
159   {
160     QUICAckFrameManager ack_manager;
161 
162     QUICEncryptionLevel level = QUICEncryptionLevel::INITIAL;
163     ack_manager.update(level, 0, 1, false);
164     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
165   }
166 
167   SECTION("QUIC handshake packet", "[quic]")
168   {
169     QUICAckFrameManager ack_manager;
170 
171     QUICEncryptionLevel level = QUICEncryptionLevel::HANDSHAKE;
172     ack_manager.update(level, 0, 1, false);
173     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
174   }
175 
176   SECTION("QUIC frame fired", "[quic]")
177   {
178     QUICAckFrameManager ack_manager;
179     QUICEncryptionLevel level = QUICEncryptionLevel::ONE_RTT;
180 
181     ack_manager.update(level, 0, 1, false);
182     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
183 
184     sleep(1);
185     Thread::get_hrtime_updated();
186     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
187   }
188 
189   SECTION("QUIC refresh frame", "[quic]")
190   {
191     QUICAckFrameManager ack_manager;
192     QUICEncryptionLevel level = QUICEncryptionLevel::ONE_RTT;
193 
194     uint8_t frame_buf[QUICFrame::MAX_INSTANCE_SIZE];
195     QUICFrame *ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
196     QUICAckFrame *frame  = static_cast<QUICAckFrame *>(ack_frame);
197     CHECK(frame == nullptr);
198 
199     // unorder frame should sent immediately
200     ack_manager.update(level, 1, 1, false);
201     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
202 
203     ack_manager.update(level, 2, 1, false);
204 
205     // Delay due to some reason, the frame is not valued any more, but still valued
206     sleep(1);
207     Thread::get_hrtime_updated();
208     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
209     ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
210     frame     = static_cast<QUICAckFrame *>(ack_frame);
211 
212     CHECK(frame->ack_block_count() == 0);
213     CHECK(frame->largest_acknowledged() == 2);
214     CHECK(frame->ack_block_section()->first_ack_block() == 1);
215     CHECK(frame->ack_block_section()->begin()->gap() == 0);
216     ack_frame->~QUICFrame();
217   }
218 }
219 
220 TEST_CASE("QUICAckFrameManager_loss_recover", "[quic]")
221 {
222   QUICAckFrameManager ack_manager;
223   QUICEncryptionLevel level = QUICEncryptionLevel::INITIAL;
224 
225   // Initial state
226   uint8_t frame_buf[QUICFrame::MAX_INSTANCE_SIZE];
227   QUICFrame *ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
228   QUICAckFrame *frame  = static_cast<QUICAckFrame *>(ack_frame);
229   CHECK(frame == nullptr);
230 
231   ack_manager.update(level, 2, 1, false);
232   ack_manager.update(level, 5, 1, false);
233   ack_manager.update(level, 6, 1, false);
234   ack_manager.update(level, 8, 1, false);
235   ack_manager.update(level, 9, 1, false);
236 
237   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
238   frame     = static_cast<QUICAckFrame *>(ack_frame);
239   CHECK(frame != nullptr);
240   CHECK(frame->ack_block_count() == 2);
241   CHECK(frame->largest_acknowledged() == 9);
242   CHECK(frame->ack_block_section()->first_ack_block() == 1);
243   CHECK(frame->ack_block_section()->begin()->gap() == 0);
244   ack_frame->~QUICFrame();
245 
246   CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
247 
248   ack_manager.update(level, 7, 1, false);
249   ack_manager.update(level, 4, 1, false);
250   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
251   frame     = static_cast<QUICAckFrame *>(ack_frame);
252   CHECK(frame != nullptr);
253   CHECK(frame->ack_block_count() == 1);
254   CHECK(frame->largest_acknowledged() == 9);
255   CHECK(frame->ack_block_section()->first_ack_block() == 5);
256   CHECK(frame->ack_block_section()->begin()->gap() == 0);
257   ack_frame->~QUICFrame();
258 }
259 
260 TEST_CASE("QUICAckFrameManager_QUICAckFrameCreator", "[quic]")
261 {
262   QUICAckFrameManager ack_manager;
263   QUICAckFrameManager::QUICAckFrameCreator packet_numbers(QUICPacketNumberSpace::Initial, &ack_manager);
264 
265   CHECK(packet_numbers.size() == 0);
266   CHECK(packet_numbers.largest_ack_number() == 0);
267   CHECK(packet_numbers.largest_ack_received_time() == 0);
268 
269   Thread::get_hrtime_updated();
270 
271   packet_numbers.push_back(3, 2, false);
272   CHECK(packet_numbers.size() == 1);
273   CHECK(packet_numbers.largest_ack_number() == 3);
274 
275   ink_hrtime ti = packet_numbers.largest_ack_received_time();
276   CHECK(packet_numbers.largest_ack_received_time() != 0);
277 
278   Thread::get_hrtime_updated();
279 
280   packet_numbers.push_back(2, 2, false);
281   CHECK(packet_numbers.size() == 2);
282   CHECK(packet_numbers.largest_ack_number() == 3);
283   CHECK(packet_numbers.largest_ack_received_time() == ti);
284 
285   Thread::get_hrtime_updated();
286 
287   packet_numbers.push_back(10, 2, false);
288   CHECK(packet_numbers.size() == 3);
289   CHECK(packet_numbers.largest_ack_number() == 10);
290   CHECK(packet_numbers.largest_ack_received_time() > ti);
291 
292   Thread::get_hrtime_updated();
293 
294   packet_numbers.clear();
295   CHECK(packet_numbers.size() == 0);
296   CHECK(packet_numbers.largest_ack_number() == 0);
297   CHECK(packet_numbers.largest_ack_received_time() == 0);
298 }
299 
300 TEST_CASE("QUICAckFrameManager lost_frame", "[quic]")
301 {
302   QUICAckFrameManager ack_manager;
303   QUICEncryptionLevel level = QUICEncryptionLevel::INITIAL;
304   uint8_t frame_buf[QUICFrame::MAX_INSTANCE_SIZE];
305 
306   // Initial state
307   QUICFrame *ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
308   QUICAckFrame *frame  = static_cast<QUICAckFrame *>(ack_frame);
309   CHECK(frame == nullptr);
310 
311   ack_manager.update(level, 2, 1, false);
312   ack_manager.update(level, 5, 1, false);
313   ack_manager.update(level, 6, 1, false);
314   ack_manager.update(level, 8, 1, false);
315   ack_manager.update(level, 9, 1, false);
316 
317   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
318   frame     = static_cast<QUICAckFrame *>(ack_frame);
319   CHECK(frame != nullptr);
320   CHECK(frame->ack_block_count() == 2);
321   CHECK(frame->largest_acknowledged() == 9);
322   CHECK(frame->ack_block_section()->first_ack_block() == 1);
323   CHECK(frame->ack_block_section()->begin()->gap() == 0);
324   ack_frame->~QUICFrame();
325 
326   ack_manager.on_frame_lost(frame->id());
327   CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
328   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
329   frame     = static_cast<QUICAckFrame *>(ack_frame);
330   CHECK(frame != nullptr);
331   CHECK(frame->ack_block_count() == 2);
332   CHECK(frame->largest_acknowledged() == 9);
333   CHECK(frame->ack_block_section()->first_ack_block() == 1);
334   CHECK(frame->ack_block_section()->begin()->gap() == 0);
335   ack_frame->~QUICFrame();
336 
337   CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
338   ack_manager.on_frame_lost(frame->id());
339   CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
340   ack_manager.update(level, 7, 1, false);
341   ack_manager.update(level, 4, 1, false);
342 
343   ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
344   frame     = static_cast<QUICAckFrame *>(ack_frame);
345   CHECK(frame != nullptr);
346   CHECK(frame->ack_block_count() == 1);
347   CHECK(frame->largest_acknowledged() == 9);
348   CHECK(frame->ack_block_section()->first_ack_block() == 5);
349   CHECK(frame->ack_block_section()->begin()->gap() == 0);
350   ack_frame->~QUICFrame();
351 
352   CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
353 }
354 
355 TEST_CASE("QUICAckFrameManager ack only packet", "[quic]")
356 {
357   SECTION("INITIAL")
358   {
359     QUICAckFrameManager ack_manager;
360     QUICEncryptionLevel level = QUICEncryptionLevel::INITIAL;
361     uint8_t frame_buf[QUICFrame::MAX_INSTANCE_SIZE];
362 
363     // Initial state
364     QUICFrame *ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
365     QUICAckFrame *frame  = static_cast<QUICAckFrame *>(ack_frame);
366     CHECK(frame == nullptr);
367 
368     ack_manager.update(level, 1, 1, false);
369     ack_manager.update(level, 2, 1, false);
370     ack_manager.update(level, 3, 1, false);
371     ack_manager.update(level, 4, 1, false);
372     ack_manager.update(level, 5, 1, false);
373 
374     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
375 
376     ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
377     frame     = static_cast<QUICAckFrame *>(ack_frame);
378     CHECK(frame != nullptr);
379     CHECK(frame->ack_block_count() == 0);
380     CHECK(frame->largest_acknowledged() == 5);
381     CHECK(frame->ack_block_section()->first_ack_block() == 4);
382     CHECK(frame->ack_block_section()->begin()->gap() == 0);
383     ack_frame->~QUICFrame();
384 
385     ack_manager.update(level, 6, 1, true);
386     ack_manager.update(level, 7, 1, true);
387     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
388   }
389 
390   SECTION("ONE_RTT")
391   {
392     QUICAckFrameManager ack_manager;
393     QUICEncryptionLevel level = QUICEncryptionLevel::ONE_RTT;
394     uint8_t frame_buf[QUICFrame::MAX_INSTANCE_SIZE];
395 
396     // Initial state
397     QUICFrame *ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
398     QUICAckFrame *frame  = static_cast<QUICAckFrame *>(ack_frame);
399     CHECK(frame == nullptr);
400 
401     ack_manager.update(level, 1, 1, false);
402     ack_manager.update(level, 2, 1, false);
403     ack_manager.update(level, 3, 1, false);
404     ack_manager.update(level, 4, 1, false);
405     ack_manager.update(level, 5, 1, false);
406 
407     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == true);
408 
409     ack_frame = ack_manager.generate_frame(frame_buf, level, UINT16_MAX, UINT16_MAX, 0, 0);
410     frame     = static_cast<QUICAckFrame *>(ack_frame);
411     CHECK(frame != nullptr);
412     CHECK(frame->ack_block_count() == 0);
413     CHECK(frame->largest_acknowledged() == 5);
414     CHECK(frame->ack_block_section()->first_ack_block() == 4);
415     CHECK(frame->ack_block_section()->begin()->gap() == 0);
416     ack_frame->~QUICFrame();
417 
418     ack_manager.update(level, 6, 1, true);
419     ack_manager.update(level, 7, 1, true);
420     CHECK(ack_manager.will_generate_frame(level, 0, true, 0) == false);
421   }
422 }
423