Skip to content

Commit 480b586

Browse files
leoossaJenkins-dev
authored andcommitted
test: add tests for AddrList
Signed-off-by: Leonard Ossa <leonard.ossa@openvpn.com>
1 parent defb8e9 commit 480b586

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

test/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_executable(coreUnitTests
3333
test_acc.cpp
3434
test_alignment.cpp
3535
test_acc_certcheck.cpp
36+
test_addrlist.cpp
3637
test_route_emulation.cpp
3738
test_log.cpp
3839
test_comp.cpp

test/unittests/test_addrlist.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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) 2024- OpenVPN Inc.
8+
//
9+
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10+
//
11+
12+
#include "test_common.hpp"
13+
#include "test_generators.hpp"
14+
15+
#include <set>
16+
#include <vector>
17+
18+
#include <openvpn/addr/addrlist.hpp>
19+
20+
RC_GTEST_PROP(AddrList, AddMarksAddressAsExisting, ())
21+
{
22+
openvpn::IP::AddrList address_list;
23+
const auto address = *rc::genIPAddr();
24+
address_list.add(address);
25+
RC_ASSERT(address_list.exists(address));
26+
}
27+
28+
RC_GTEST_PROP(AddrList, AddIsIdempotent, ())
29+
{
30+
openvpn::IP::AddrList list;
31+
const auto addr = *rc::genIPAddr();
32+
list.add(addr);
33+
list.add(addr);
34+
RC_ASSERT(list.size() == 1U);
35+
RC_ASSERT(list[0] == addr);
36+
}
37+
38+
RC_GTEST_PROP(AddrList, AddDistinctAddressesKeepsUniqueCount, ())
39+
{
40+
const auto addrs = *rc::gen::container<std::vector<openvpn::IP::Addr>>(rc::genIPAddr());
41+
const std::set unique_addrs(addrs.begin(), addrs.end());
42+
openvpn::IP::AddrList list;
43+
for (const auto &addr : addrs)
44+
{
45+
list.add(addr);
46+
}
47+
RC_ASSERT(list.size() == unique_addrs.size());
48+
}
49+
50+
RC_GTEST_PROP(AddrList, ExistsMatchesMembership, ())
51+
{
52+
const auto addrs = *rc::gen::container<std::vector<openvpn::IP::Addr>>(rc::genIPAddr());
53+
const std::set unique_addrs(addrs.begin(), addrs.end());
54+
openvpn::IP::AddrList list;
55+
for (const auto &addr : addrs)
56+
{
57+
list.add(addr);
58+
}
59+
const auto probe = *rc::gen::oneOf(rc::gen::elementOf(unique_addrs), rc::genIPAddr());
60+
const bool expected = unique_addrs.contains(probe);
61+
RC_ASSERT(list.exists(probe) == expected);
62+
}

test/unittests/test_generators.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,5 +917,13 @@ inline auto genDNSOptions() -> Gen<openvpn::DnsOptions>
917917
gen::set(&openvpn::DnsOptions::servers, generateDnsServerMap()));
918918
}
919919

920+
inline auto genIPAddr() -> Gen<openvpn::IP::Addr>
921+
{
922+
return gen::map(gen::oneOf(IPv4Address(), IPv6Address()),
923+
[](const std::string &ip)
924+
{
925+
return openvpn::IP::Addr::from_string(ip);
926+
});
927+
}
920928
} // namespace rc
921929
#endif // TEST_GENERATORS_HPP

0 commit comments

Comments
 (0)