OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_pktid_control.cpp
Go to the documentation of this file.
1#include "test_common.hpp"
2
4
5using namespace openvpn;
6
15
16
17template <typename PIDRecv>
18void testcase(PIDRecv &pr,
20 const PacketIDControl::time_t pkt_time,
21 const PacketIDControl::id_t pkt_id,
22 const Error::Type expected_status)
23{
24 const PacketIDControlConstruct pid(pkt_time, pkt_id);
25 const Error::Type status = pr.do_test_add(pid, t, true);
26 // OPENVPN_LOG("[" << t << "] id=" << pkt_id << " time=" << pkt_time << ' ' << Error::name(status));
27 ASSERT_EQ(status, expected_status);
28}
29
30TEST(misc, pktid_test_control)
31{
34 PIDRecv pr;
35 pr.init("test", 0, stats);
36
37 testcase(pr, 0, 0, 0, Error::PKTID_INVALID);
38 testcase(pr, 1, 0, 1, Error::SUCCESS);
39 testcase(pr, 1, 0, 1, Error::PKTID_REPLAY); /* replay */
40 testcase(pr, 2, 0, 2, Error::SUCCESS);
41 testcase(pr, 3, 0, 4, Error::SUCCESS);
42 testcase(pr, 4, 0, 1, Error::PKTID_REPLAY); /* replay */
43 testcase(pr, 5, 0, 3, Error::SUCCESS);
44
45 testcase(pr, 6, 0, 8, Error::SUCCESS);
46 testcase(pr, 10, 0, 5, Error::SUCCESS);
47 testcase(pr, 15, 0, 7, Error::PKTID_EXPIRE); /* expire backtrack */
48
49 testcase(pr, 20, 0, 127, Error::SUCCESS);
50 testcase(pr, 20, 0, 127, Error::PKTID_REPLAY);
51 testcase(pr, 21, 0, 128, Error::SUCCESS);
52 testcase(pr, 22, 0, 64, Error::PKTID_BACKTRACK); /* large backtrack */
53 testcase(pr, 23, 0, 65, Error::SUCCESS);
54 testcase(pr, 24, 0, 66, Error::SUCCESS);
55
56 testcase(pr, 30, 10, 0, Error::PKTID_INVALID);
57 testcase(pr, 31, 10, 2, Error::SUCCESS);
58 testcase(pr, 32, 10, 1, Error::SUCCESS);
59 testcase(pr, 33, 9, 3, Error::PKTID_TIME_BACKTRACK); /* time backtrack */
60 testcase(pr, 33, 0, 3, Error::PKTID_TIME_BACKTRACK); /* time backtrack */
61
62 testcase(pr, 40, 10, 0xfffffffe, Error::SUCCESS);
63 testcase(pr, 41, 10, 0xffffffff, Error::SUCCESS);
64 testcase(pr, 42, 10, 0, Error::PKTID_INVALID); /* wrap */
65
66 testcase(pr, 50, 11, 1, Error::SUCCESS);
67 testcase(pr, 51, 11, 2, Error::SUCCESS);
68 testcase(pr, 52, 11, 3, Error::SUCCESS);
69 testcase(pr, 53, 11, 3, Error::PKTID_REPLAY); /* replay */
70 testcase(pr, 54, 11, 10, Error::SUCCESS);
71 testcase(pr, 55, 11, 9, Error::SUCCESS);
72 testcase(pr, 56, 11, 1, Error::PKTID_REPLAY); /* replay */
73 testcase(pr, 57, 11, 8, Error::SUCCESS);
74 testcase(pr, 58, 11, 4, Error::SUCCESS);
75 testcase(pr, 63, 11, 5, Error::PKTID_EXPIRE); /* expire backtrack */
76
77 testcase(pr, 70, 15, 1, Error::SUCCESS);
78 testcase(pr, 70, 15, 3, Error::SUCCESS);
79 testcase(pr, 70, 15, 2, Error::SUCCESS);
80
81 testcase(pr, 80, 15, 50, Error::SUCCESS);
82 testcase(pr, 80, 15, 80, Error::SUCCESS);
83 testcase(pr, 81, 15, 127, Error::SUCCESS);
84 testcase(pr, 82, 15, 128, Error::SUCCESS);
85 testcase(pr, 83, 15, 64, Error::PKTID_BACKTRACK); /* large backtrack */
86 testcase(pr, 84, 15, 65, Error::SUCCESS);
87 testcase(pr, 85, 15, 66, Error::SUCCESS);
88}
89
90template <unsigned int ORDER, unsigned int EXPIRE>
91void perfiter(const long n,
92 const long range,
93 const long step,
94 const long iter_per_step_pre,
95 long &count)
96{
98
99 const long iter_per_step = iter_per_step_pre * step;
100 // OPENVPN_LOG("ITER order=" << ORDER << " n=" << n << " range=" << range << " step=" << step << " iter_per_step="
101 // << iter_per_step);
102
103 constexpr PacketIDControl::time_t pkt_time = 1234;
104
105 MTRand urand;
106 std::vector<bool> bv(n);
107 long high = 0;
108 SessionStats::Ptr stats(new SessionStats());
109 PIDRecv pr;
110 pr.init("test", 0, stats);
111
112 for (long i = 1; i < n; i += step)
113 {
114 for (long j = 0; j < iter_per_step; ++j)
115 {
116 const long delta = long(urand.randrange32(static_cast<uint32_t>(range))) - range / 2;
117 const long id = i + delta;
118 if (id >= 0 && id < n)
119 {
120 if (id > high)
121 high = id;
123 if (!id)
125 else if (high - id >= (const long)PIDRecv::REPLAY_WINDOW_SIZE)
127 else if (bv[id])
129 const PacketIDControlConstruct pid(0, static_cast<unsigned>(id));
130 const Error::Type result = pr.do_test_add(pid, pkt_time, true);
131 ++count;
132#define INFO "i=" << i << " id=" << id << " high=" << high << " result=" << Error::name(result) << " expected=" << Error::name(expected)
133 // OPENVPN_LOG(INFO);
134 ASSERT_EQ(result, expected) << INFO;
136 bv[id] = true;
137 }
138 }
139 }
140}
141
142template <unsigned int ORDER, unsigned int EXPIRE>
143void perf(long &count)
144{
146
147 perfiter<ORDER, EXPIRE>(20000, PIDRecv::REPLAY_WINDOW_SIZE * 3, 1, 10, count);
148 perfiter<ORDER, EXPIRE>(20000, PIDRecv::REPLAY_WINDOW_SIZE * 3, PIDRecv::REPLAY_WINDOW_SIZE / 2, 10, count);
149 perfiter<ORDER, EXPIRE>(20000, PIDRecv::REPLAY_WINDOW_SIZE * 2, 1, 10, count);
150 perfiter<ORDER, EXPIRE>(20000, PIDRecv::REPLAY_WINDOW_SIZE * 2, PIDRecv::REPLAY_WINDOW_SIZE / 2, 10, count);
151 perfiter<ORDER, EXPIRE>(20000, 16, 1, 10, count);
152 perfiter<ORDER, EXPIRE>(20000, 16, PIDRecv::REPLAY_WINDOW_SIZE / 2, 10, count);
153 perfiter<ORDER, EXPIRE>(20000, 4, 1, 10, count);
154 perfiter<ORDER, EXPIRE>(20000, 4, PIDRecv::REPLAY_WINDOW_SIZE / 2, 10, count);
155}
156
157TEST(misc, pktid_control_perf)
158{
159 long count = 0;
160 perf<3, 5>(count);
161 perf<6, 5>(count);
162 perf<8, 5>(count);
163 // ASSERT_EQ(4746439, count);
164}
std::uint32_t randrange32(const std::uint32_t end)
Return a uniformly distributed random number in the range [0, end)
Definition randapi.hpp:147
@ PKTID_TIME_BACKTRACK
Definition error.hpp:109
PacketIDControlConstruct(const PacketIDControl::time_t v_time=PacketIDControl::time_t(0), const PacketIDControl::id_t v_id=PacketIDControl::id_t(0))
const std::string expected
void perf()
void testcase(PIDRecv &pr, const PacketIDControl::time_t t, const PacketIDControl::time_t pkt_time, const PacketIDControl::id_t pkt_id, const Error::Type expected_status)
TEST(misc, pktid_test_control)
void perfiter(const long n, const long range, const long step, const long iter_per_step_pre, long &count)
#define INFO