2022-11-21 01:04:34 +01:00
|
|
|
// OpenVPN -- An application to securely tunnel IP networks
|
|
|
|
// over a single port, with support for SSL/TLS-based
|
|
|
|
// session authentication and key exchange,
|
|
|
|
// packet encryption, packet authentication, and
|
|
|
|
// packet compression.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2012-2022 OpenVPN Inc.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License Version 3
|
|
|
|
// as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program in the COPYING file.
|
|
|
|
|
|
|
|
#include "test_common.h"
|
|
|
|
|
|
|
|
#include <openvpn/reliable/relack.hpp>
|
|
|
|
|
|
|
|
using namespace openvpn;
|
|
|
|
using namespace openvpn::reliable;
|
2023-03-30 16:14:01 +02:00
|
|
|
namespace orel = openvpn::reliable;
|
2022-11-21 01:04:34 +01:00
|
|
|
|
|
|
|
TEST(relack, test_size_1)
|
|
|
|
{
|
|
|
|
constexpr size_t ACK_CNT = 11;
|
|
|
|
|
|
|
|
auto ra = ReliableAck{};
|
2023-03-30 16:14:01 +02:00
|
|
|
for (auto i = orel::id_t(1); i <= ACK_CNT; ++i)
|
2022-11-21 01:04:34 +01:00
|
|
|
ra.push_back(i);
|
|
|
|
EXPECT_EQ(ra.size(), ACK_CNT);
|
2023-02-14 14:52:41 +01:00
|
|
|
EXPECT_EQ(ra.resend_size(), size_t{0});
|
2022-11-21 01:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(relack, test_prepend_1)
|
|
|
|
{
|
|
|
|
constexpr size_t ACK_CNT = 11;
|
|
|
|
|
|
|
|
auto ra = ReliableAck{};
|
2023-03-30 16:14:01 +02:00
|
|
|
for (auto i = orel::id_t{1}; i <= ACK_CNT; ++i)
|
2022-11-21 01:04:34 +01:00
|
|
|
ra.push_back(i);
|
|
|
|
EXPECT_EQ(ra.size(), ACK_CNT);
|
|
|
|
|
|
|
|
constexpr size_t storageSize = 1024;
|
|
|
|
unsigned char storage[storageSize];
|
|
|
|
|
|
|
|
{
|
|
|
|
auto buf = Buffer(storage, storageSize, false);
|
|
|
|
buf.init_headroom(storageSize / 2);
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2022-11-21 01:04:34 +01:00
|
|
|
// Add 4 packets to a CONTROL packet, should reduce number by 4
|
|
|
|
ra.prepend(buf, false);
|
|
|
|
EXPECT_EQ(ra.size(), ACK_CNT - 4);
|
2023-02-14 14:52:41 +01:00
|
|
|
EXPECT_EQ(ra.resend_size(), size_t{4});
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2022-11-21 01:04:34 +01:00
|
|
|
// Add packets to an ACK_V1 packet, should reduce number by up to 8
|
|
|
|
ra.prepend(buf, true);
|
2023-02-14 14:52:41 +01:00
|
|
|
EXPECT_EQ(ra.size(), size_t{0});
|
|
|
|
EXPECT_EQ(ra.resend_size(), size_t{8});
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
2022-11-21 01:04:34 +01:00
|
|
|
|
2023-01-11 20:43:22 +01:00
|
|
|
{
|
2022-11-21 01:04:34 +01:00
|
|
|
auto buf = Buffer(storage, storageSize, false);
|
|
|
|
buf.init_headroom(storageSize / 2);
|
|
|
|
|
|
|
|
ra.prepend(buf, false); // resending should not change array sizes
|
|
|
|
EXPECT_EQ(ra.size(), size_t{0});
|
2023-02-14 14:52:41 +01:00
|
|
|
EXPECT_EQ(ra.resend_size(), size_t{8});
|
2022-11-21 01:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto buf = Buffer(storage, storageSize, false);
|
|
|
|
buf.init_headroom(storageSize / 2);
|
|
|
|
|
|
|
|
ra.prepend(buf, false);
|
|
|
|
EXPECT_EQ(ra.size(), size_t{0});
|
2023-02-14 14:52:41 +01:00
|
|
|
EXPECT_EQ(ra.resend_size(), size_t{8});
|
2022-11-21 01:04:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RelSendMck
|
|
|
|
{
|
2023-03-30 16:14:01 +02:00
|
|
|
void ack(orel::id_t id)
|
2022-11-21 01:04:34 +01:00
|
|
|
{
|
|
|
|
mAcks.push_back(id);
|
|
|
|
};
|
2023-03-30 16:14:01 +02:00
|
|
|
std::vector<orel::id_t> mAcks;
|
2022-11-21 01:04:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(relack, test_ack_1)
|
|
|
|
{
|
|
|
|
constexpr size_t ACK_CNT = 9;
|
|
|
|
|
|
|
|
auto ra = ReliableAck{};
|
2023-03-30 16:14:01 +02:00
|
|
|
for (auto i = orel::id_t{1}; i <= ACK_CNT; ++i)
|
2022-11-21 01:04:34 +01:00
|
|
|
ra.push_back(i);
|
|
|
|
EXPECT_EQ(ra.size(), ACK_CNT);
|
|
|
|
|
|
|
|
constexpr size_t storageSize = 1024;
|
|
|
|
unsigned char storage[storageSize];
|
|
|
|
|
|
|
|
auto buf = Buffer(storage, storageSize, false);
|
|
|
|
buf.init_headroom(storageSize / 2);
|
|
|
|
|
|
|
|
ra.prepend(buf, false);
|
|
|
|
EXPECT_EQ(ra.size(), ACK_CNT - 4);
|
|
|
|
|
|
|
|
RelSendMck send;
|
|
|
|
ra.ack(send, buf, true);
|
|
|
|
|
|
|
|
for (auto i = size_t{0}; i < send.mAcks.size(); ++i)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(send.mAcks[i], 4 - i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(relack, test_ack_2)
|
|
|
|
{
|
|
|
|
constexpr size_t ACK_CNT = 9;
|
|
|
|
|
|
|
|
auto ra = ReliableAck{};
|
2023-03-30 16:14:01 +02:00
|
|
|
for (auto i = orel::id_t(1); i <= ACK_CNT; ++i)
|
2022-11-21 01:04:34 +01:00
|
|
|
ra.push_back(i);
|
|
|
|
EXPECT_EQ(ra.size(), ACK_CNT);
|
|
|
|
|
|
|
|
constexpr size_t storageSize = 1024;
|
|
|
|
unsigned char storage[storageSize];
|
|
|
|
|
2023-01-11 20:43:22 +01:00
|
|
|
{
|
2022-11-21 01:04:34 +01:00
|
|
|
auto buf = Buffer(storage, storageSize, false);
|
|
|
|
buf.init_headroom(storageSize / 2);
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2023-02-14 14:52:41 +01:00
|
|
|
EXPECT_EQ(ra.size(), size_t{9});
|
|
|
|
EXPECT_EQ(ra.resend_size(), size_t{0});
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2022-11-21 01:04:34 +01:00
|
|
|
ra.prepend(buf, true);
|
2023-02-14 14:52:41 +01:00
|
|
|
EXPECT_EQ(ra.size(), size_t{1});
|
|
|
|
EXPECT_EQ(ra.resend_size(), size_t{8});
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2022-11-21 01:04:34 +01:00
|
|
|
RelSendMck send{};
|
|
|
|
auto num = ra.ack(send, buf, false);
|
|
|
|
EXPECT_EQ(send.mAcks.size(), size_t{0});
|
2023-02-14 14:52:41 +01:00
|
|
|
EXPECT_EQ(num, size_t{8});
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2022-11-21 01:04:34 +01:00
|
|
|
RelSendMck send2;
|
|
|
|
ra.prepend(buf, true);
|
|
|
|
num = ra.ack(send2, buf, true);
|
2023-02-14 14:52:41 +01:00
|
|
|
EXPECT_EQ(num, size_t{8});
|
2022-11-21 01:04:34 +01:00
|
|
|
EXPECT_EQ(send2.mAcks.size(), size_t{8});
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
2022-11-21 01:04:34 +01:00
|
|
|
}
|