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 "QUICPinger.h"
27 
28 static constexpr QUICEncryptionLevel level = QUICEncryptionLevel::ONE_RTT;
29 static uint8_t frame[1024]                 = {0};
30 
31 TEST_CASE("QUICPinger", "[quic]")
32 {
33   SECTION("request and cancel")
34   {
35     QUICPinger pinger;
36     pinger.request();
37     REQUIRE(pinger.count() == 1);
38     pinger.request();
39     REQUIRE(pinger.count() == 2);
40     pinger.cancel();
41     REQUIRE(pinger.count() == 1);
42     REQUIRE(pinger.generate_frame(frame, level, UINT64_MAX, UINT16_MAX, 0, 0) != nullptr);
43     REQUIRE(pinger.count() == 0);
44   }
45 
46   SECTION("generate PING Frame twice")
47   {
48     QUICPinger pinger;
49     pinger.request();
50     REQUIRE(pinger.count() == 1);
51     pinger.request();
52     REQUIRE(pinger.count() == 2);
53     REQUIRE(pinger.will_generate_frame(level, UINT64_MAX, false, 0) == true);
54     REQUIRE(pinger.count() == 2);
55     REQUIRE(pinger.will_generate_frame(level, UINT64_MAX, false, 0) == false);
56     REQUIRE(pinger.count() == 2);
57   }
58 
59   SECTION("don't generate frame when packet is ack_elicting")
60   {
61     QUICPinger pinger;
62     pinger.request();
63     REQUIRE(pinger.count() == 1);
64     pinger.request();
65     REQUIRE(pinger.count() == 2);
66     REQUIRE(pinger.will_generate_frame(level, UINT64_MAX, true, 0) == false);
67     REQUIRE(pinger.count() == 1);
68     REQUIRE(pinger.will_generate_frame(level, UINT64_MAX, true, 1) == false);
69     REQUIRE(pinger.count() == 0);
70   }
71 
72   SECTION("generating PING Frame for next continuos un-ack-eliciting packets")
73   {
74     QUICPinger pinger;
75     REQUIRE(pinger.will_generate_frame(level, UINT64_MAX, false, 0) == true);
76     REQUIRE(pinger.count() == 1);
77     REQUIRE(pinger.will_generate_frame(level, UINT64_MAX, true, 1) == false);
78     REQUIRE(pinger.count() == 0);
79     REQUIRE(pinger.will_generate_frame(level, UINT64_MAX, false, 2) == false);
80     REQUIRE(pinger.count() == 0);
81     REQUIRE(pinger.will_generate_frame(level, UINT64_MAX, false, 3) == true);
82     REQUIRE(pinger.count() == 1);
83   }
84 
85   SECTION("don't send PING Frame for empty packet")
86   {
87     QUICPinger pinger;
88     REQUIRE(pinger.will_generate_frame(level, 0, false, 0) == false);
89     REQUIRE(pinger.count() == 0);
90     REQUIRE(pinger.will_generate_frame(level, UINT64_MAX, false, 1) == true);
91     REQUIRE(pinger.count() == 1);
92     REQUIRE(pinger.will_generate_frame(level, UINT64_MAX, true, 2) == false);
93     REQUIRE(pinger.count() == 0);
94     REQUIRE(pinger.will_generate_frame(level, UINT64_MAX, false, 3) == false);
95     REQUIRE(pinger.count() == 0);
96     REQUIRE(pinger.will_generate_frame(level, 0, false, 4) == false);
97     REQUIRE(pinger.count() == 0);
98     REQUIRE(pinger.will_generate_frame(level, 1, false, 5) == true);
99     REQUIRE(pinger.count() == 1);
100   }
101 }
102