OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_relack.cpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12
13#include "test_common.hpp"
14
16
17using namespace openvpn;
18using namespace openvpn::reliable;
19namespace orel = openvpn::reliable;
20
21TEST(relack, test_size_1)
22{
23 constexpr size_t ACK_CNT = 11;
24
25 auto ra = ReliableAck{};
26 for (auto i = orel::id_t(1); i <= ACK_CNT; ++i)
27 ra.push_back(i);
28 EXPECT_EQ(ra.size(), ACK_CNT);
29 EXPECT_EQ(ra.resend_size(), size_t{0});
30}
31
32TEST(relack, test_prepend_1)
33{
34 constexpr size_t ACK_CNT = 11;
35
36 auto ra = ReliableAck{};
37 for (auto i = orel::id_t{1}; i <= ACK_CNT; ++i)
38 ra.push_back(i);
39 EXPECT_EQ(ra.size(), ACK_CNT);
40
41 constexpr size_t storageSize = 1024;
42 unsigned char storage[storageSize];
43
44 {
45 auto buf = Buffer(storage, storageSize, false);
46 buf.init_headroom(storageSize / 2);
47
48 // Add 4 packets to a CONTROL packet, should reduce number by 4
49 ra.prepend(buf, false);
50 EXPECT_EQ(ra.size(), ACK_CNT - 4);
51 EXPECT_EQ(ra.resend_size(), size_t{4});
52
53 // Add packets to an ACK_V1 packet, should reduce number by up to 8
54 ra.prepend(buf, true);
55 EXPECT_EQ(ra.size(), size_t{0});
56 EXPECT_EQ(ra.resend_size(), size_t{8});
57 }
58
59 {
60 auto buf = Buffer(storage, storageSize, false);
61 buf.init_headroom(storageSize / 2);
62
63 ra.prepend(buf, false); // resending should not change array sizes
64 EXPECT_EQ(ra.size(), size_t{0});
65 EXPECT_EQ(ra.resend_size(), size_t{8});
66 }
67
68 {
69 auto buf = Buffer(storage, storageSize, false);
70 buf.init_headroom(storageSize / 2);
71
72 ra.prepend(buf, false);
73 EXPECT_EQ(ra.size(), size_t{0});
74 EXPECT_EQ(ra.resend_size(), size_t{8});
75 }
76}
77
79{
80 void ack(orel::id_t id)
81 {
82 mAcks.push_back(id);
83 };
84 std::vector<orel::id_t> mAcks;
85};
86
87TEST(relack, test_ack_1)
88{
89 constexpr size_t ACK_CNT = 9;
90
91 auto ra = ReliableAck{};
92 for (auto i = orel::id_t{1}; i <= ACK_CNT; ++i)
93 ra.push_back(i);
94 EXPECT_EQ(ra.size(), ACK_CNT);
95
96 constexpr size_t storageSize = 1024;
97 unsigned char storage[storageSize];
98
99 auto buf = Buffer(storage, storageSize, false);
100 buf.init_headroom(storageSize / 2);
101
102 ra.prepend(buf, false);
103 EXPECT_EQ(ra.size(), ACK_CNT - 4);
104
105 RelSendMck send;
106 ra.ack(send, buf, true);
107
108 for (auto i = size_t{0}; i < send.mAcks.size(); ++i)
109 {
110 EXPECT_EQ(send.mAcks[i], 4 - i);
111 }
112}
113
114TEST(relack, test_ack_2)
115{
116 constexpr size_t ACK_CNT = 9;
117
118 auto ra = ReliableAck{};
119 for (auto i = orel::id_t(1); i <= ACK_CNT; ++i)
120 ra.push_back(i);
121 EXPECT_EQ(ra.size(), ACK_CNT);
122
123 constexpr size_t storageSize = 1024;
124 unsigned char storage[storageSize];
125
126 {
127 auto buf = Buffer(storage, storageSize, false);
128 buf.init_headroom(storageSize / 2);
129
130 EXPECT_EQ(ra.size(), size_t{9});
131 EXPECT_EQ(ra.resend_size(), size_t{0});
132
133 ra.prepend(buf, true);
134 EXPECT_EQ(ra.size(), size_t{1});
135 EXPECT_EQ(ra.resend_size(), size_t{8});
136
137 RelSendMck send{};
138 auto num = ra.ack(send, buf, false);
139 EXPECT_EQ(send.mAcks.size(), size_t{0});
140 EXPECT_EQ(num, size_t{8});
141
142 RelSendMck send2;
143 ra.prepend(buf, true);
144 num = ra.ack(send2, buf, true);
145 EXPECT_EQ(num, size_t{8});
146 EXPECT_EQ(send2.mAcks.size(), size_t{8});
147 }
148}
void push_back(id_t value)
Definition relack.hpp:39
std::uint32_t id_t
Definition relcommon.hpp:22
BufferType< unsigned char > Buffer
Definition buffer.hpp:1895
std::vector< orel::id_t > mAcks
void ack(orel::id_t id)
TEST(relack, test_size_1)