Skip to content

Commit c9a0ab3

Browse files
authored
Migrate from catch2v2 to catch2v3 (#692)
* migrate from catch2v2 to catch2v3 and implement VisorTest helper lib * Replach benchmark with catch2 benchmark
1 parent 5e15f15 commit c9a0ab3

File tree

89 files changed

+409
-892
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+409
-892
lines changed

conanfile.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[requires]
2-
benchmark/1.8.3
3-
catch2/2.13.10
2+
catch2/3.4.0
43
corrade/2020.06
54
cpp-httplib/0.14.0
65
docopt.cpp/0.6.3

libs/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
message(STATUS "Custom Libraries")
22

3+
add_subdirectory(visor_test)
34
add_subdirectory(visor_transaction)
45
add_subdirectory(visor_tcp)
56
add_subdirectory(visor_dns)

libs/visor_dns/CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,16 @@ target_link_libraries(VisorLibDns
2424
${CONAN_LIBS_FMT}
2525
)
2626

27-
add_subdirectory(tests)
27+
## TEST SUITE
28+
add_executable(unit-tests-visor-dns
29+
tests/test_dns.cpp
30+
tests/benchmark_dns.cpp)
31+
32+
target_link_libraries(unit-tests-visor-dns
33+
PRIVATE
34+
Visor::Lib::Dns
35+
${CONAN_LIBS_CATCH2})
36+
37+
add_test(NAME unit-tests-visor-dns
38+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/visor_dns
39+
COMMAND unit-tests-visor-dns)

libs/visor_dns/dns.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "DnsResource.h"
99
#include "DnsResourceData.h"
1010
#include <string>
11+
#include <string_view>
1112
#include <unordered_map>
1213

1314
namespace visor::lib::dns {

libs/visor_dns/tests/CMakeLists.txt

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* This Source Code Form is subject to the terms of the Mozilla Public
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
#include <catch2/benchmark/catch_benchmark.hpp>
5+
#include <catch2/catch_test_macros.hpp>
46

5-
#include "benchmark/benchmark.h"
67
#include "dns.h"
78
#ifdef __GNUC__
89
#pragma GCC diagnostic push
@@ -21,66 +22,65 @@
2122

2223
using namespace visor::lib::dns;
2324

24-
static void BM_aggregateDomain(benchmark::State &state)
25+
void BM_aggregateDomain(const std::string &domain)
2526
{
2627
AggDomainResult result;
27-
std::string domain{"biz.foo.bar.com"};
28-
for (auto _ : state) {
29-
result = aggregateDomain(domain);
30-
}
31-
}
32-
BENCHMARK(BM_aggregateDomain);
33-
34-
static void BM_aggregateDomainLong(benchmark::State &state)
35-
{
36-
AggDomainResult result;
37-
std::string domain{"long1.long2.long3.long4.long5.long6.long7.long8.biz.foo.bar.com"};
38-
for (auto _ : state) {
39-
result = aggregateDomain(domain);
40-
}
28+
result = aggregateDomain(domain);
4129
}
4230

43-
BENCHMARK(BM_aggregateDomainLong);
44-
45-
static void BM_pcapReadNoParse(benchmark::State &state)
31+
void BM_pcapReadNoParse()
4632
{
33+
auto reader = pcpp::IFileReaderDevice::getReader("tests/dns_udp_tcp_random.pcap");
4734

48-
for (auto _ : state) {
49-
auto reader = pcpp::IFileReaderDevice::getReader("fixtures/dns_udp_tcp_random.pcap");
50-
51-
if (!reader->open()) {
52-
throw std::runtime_error("Cannot open pcap/pcapng file");
53-
}
54-
55-
pcpp::RawPacket rawPacket;
56-
while (reader->getNextPacket(rawPacket)) {
57-
}
35+
if (!reader->open()) {
36+
throw std::runtime_error("Cannot open pcap/pcapng file");
37+
}
5838

59-
reader->close();
60-
delete reader;
39+
pcpp::RawPacket rawPacket;
40+
while (reader->getNextPacket(rawPacket)) {
6141
}
42+
43+
reader->close();
44+
delete reader;
6245
}
63-
BENCHMARK(BM_pcapReadNoParse);
6446

65-
static void BM_pcapReadParse1(benchmark::State &state)
47+
void BM_pcapReadParse()
6648
{
6749

68-
for (auto _ : state) {
69-
auto reader = pcpp::IFileReaderDevice::getReader("fixtures/dns_udp_tcp_random.pcap");
70-
71-
if (!reader->open()) {
72-
throw std::runtime_error("Cannot open pcap/pcapng file");
73-
}
50+
auto reader = pcpp::IFileReaderDevice::getReader("tests/dns_udp_tcp_random.pcap");
7451

75-
pcpp::RawPacket rawPacket;
76-
while (reader->getNextPacket(rawPacket)) {
77-
pcpp::Packet packet(&rawPacket, pcpp::OsiModelTransportLayer);
78-
}
52+
if (!reader->open()) {
53+
throw std::runtime_error("Cannot open pcap/pcapng file");
54+
}
7955

80-
reader->close();
81-
delete reader;
56+
pcpp::RawPacket rawPacket;
57+
while (reader->getNextPacket(rawPacket)) {
58+
pcpp::Packet packet(&rawPacket, pcpp::OsiModelTransportLayer);
8259
}
60+
61+
reader->close();
62+
delete reader;
8363
}
84-
BENCHMARK(BM_pcapReadParse1);
8564

86-
BENCHMARK_MAIN();
65+
TEST_CASE("DNS benchmark")
66+
{
67+
BENCHMARK("Aggregate Domain")
68+
{
69+
return BM_aggregateDomain("biz.foo.bar.com");
70+
};
71+
72+
BENCHMARK("Aggregate Domain Long")
73+
{
74+
return BM_aggregateDomain("long1.long2.long3.long4.long5.long6.long7.long8.biz.foo.bar.com");
75+
};
76+
77+
BENCHMARK("Pcap Read No Parse")
78+
{
79+
return BM_pcapReadNoParse();
80+
};
81+
82+
BENCHMARK("Pcap Read No Parse")
83+
{
84+
return BM_pcapReadParse();
85+
};
86+
}
1.69 MB
Binary file not shown.

libs/visor_dns/tests/main.cpp

Lines changed: 0 additions & 24 deletions
This file was deleted.

libs/visor_dns/tests/test_dns.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,119 @@
1-
#include "catch2/catch.hpp"
1+
#include <catch2/catch_test_macros.hpp>
22

33
#include "dns.h"
44

55
using namespace visor::lib::dns;
66

7+
static std::pair<std::string, std::string> convert(const AggDomainResult &result)
8+
{
9+
return {std::string(result.first), std::string(result.second)};
10+
}
11+
712
TEST_CASE("DNS Utilities", "[dns]")
813
{
14+
std::pair<std::string, std::string> result;
15+
std::string domain;
916

1017
SECTION("aggregateDomain")
1118
{
12-
AggDomainResult result;
13-
std::string domain;
14-
1519
domain = "biz.foo.bar.com";
16-
result = aggregateDomain(domain);
20+
result = convert(aggregateDomain(domain));
1721
CHECK(result.first == ".bar.com");
1822
CHECK(result.second == ".foo.bar.com");
1923

2024
domain = "a.com";
21-
result = aggregateDomain(domain);
25+
result = convert(aggregateDomain(domain));
2226
CHECK(result.first == "a.com");
2327
CHECK(result.second == "");
2428

2529
domain = "abcdefg.com.";
26-
result = aggregateDomain(domain);
30+
result = convert(aggregateDomain(domain));
2731
CHECK(result.first == "abcdefg.com.");
2832
CHECK(result.second == "");
2933

3034
domain = "foo.bar.com";
31-
result = aggregateDomain(domain);
35+
result = convert(aggregateDomain(domain));
3236
CHECK(result.first == ".bar.com");
3337
CHECK(result.second == "foo.bar.com");
3438

3539
domain = ".";
36-
result = aggregateDomain(domain);
40+
result = convert(aggregateDomain(domain));
3741
CHECK(result.first == ".");
3842
CHECK(result.second == "");
3943

4044
domain = "..";
41-
result = aggregateDomain(domain);
45+
result = convert(aggregateDomain(domain));
4246
CHECK(result.first == "..");
4347
CHECK(result.second == "");
4448

4549
domain = "a";
46-
result = aggregateDomain(domain);
50+
result = convert(aggregateDomain(domain));
4751
CHECK(result.first == "a");
4852
CHECK(result.second == "");
4953

5054
domain = "a.";
51-
result = aggregateDomain(domain);
55+
result = convert(aggregateDomain(domain));
5256
CHECK(result.first == "a.");
5357
CHECK(result.second == "");
5458

5559
domain = "foo.bar.com.";
56-
result = aggregateDomain(domain);
60+
result = convert(aggregateDomain(domain));
5761
CHECK(result.first == ".bar.com.");
5862
CHECK(result.second == "foo.bar.com.");
5963

6064
domain = ".foo.bar.com";
61-
result = aggregateDomain(domain);
65+
result = convert(aggregateDomain(domain));
6266
CHECK(result.first == ".bar.com");
6367
CHECK(result.second == ".foo.bar.com");
6468

6569
domain = "a.b.c";
66-
result = aggregateDomain(domain);
70+
result = convert(aggregateDomain(domain));
6771
CHECK(result.first == ".b.c");
6872
CHECK(result.second == "a.b.c");
6973

7074
domain = ".b.c";
71-
result = aggregateDomain(domain);
75+
result = convert(aggregateDomain(domain));
7276
CHECK(result.first == ".b.c");
7377
CHECK(result.second == "");
7478
}
7579

7680
SECTION("aggregateDomain with static suffix")
7781
{
78-
AggDomainResult result;
79-
std::string domain;
8082
std::string static_suffix;
8183

8284
domain = "biz.foo.bar.com";
8385
static_suffix = ".bar.com";
84-
result = aggregateDomain(domain, static_suffix.size());
86+
result = convert(aggregateDomain(domain, static_suffix.size()));
8587
CHECK(result.first == ".foo.bar.com");
8688
CHECK(result.second == "biz.foo.bar.com");
8789

8890
domain = "biz.foo.bar.com";
8991
static_suffix = "bar.com";
90-
result = aggregateDomain(domain, static_suffix.size());
92+
result = convert(aggregateDomain(domain, static_suffix.size()));
9193
CHECK(result.first == ".foo.bar.com");
9294
CHECK(result.second == "biz.foo.bar.com");
9395

9496
domain = "biz.foo.bar.com";
9597
static_suffix = "foo.bar.com";
96-
result = aggregateDomain(domain, static_suffix.size());
98+
result = convert(aggregateDomain(domain, static_suffix.size()));
9799
CHECK(result.first == "biz.foo.bar.com");
98100
CHECK(result.second == "");
99101

100102
domain = "foo.bar.com.";
101103
static_suffix = "biz.foo.bar.com";
102-
result = aggregateDomain(domain, static_suffix.size());
104+
result = convert(aggregateDomain(domain, static_suffix.size()));
103105
CHECK(result.first == ".bar.com.");
104106
CHECK(result.second == "foo.bar.com.");
105107

106108
domain = "www.google.co.uk";
107109
static_suffix = ".co.uk";
108-
result = aggregateDomain(domain, static_suffix.size());
110+
result = convert(aggregateDomain(domain, static_suffix.size()));
109111
CHECK(result.first == ".google.co.uk");
110112
CHECK(result.second == "www.google.co.uk");
111113

112114
domain = "www.google.co.uk";
113115
static_suffix = "google.co.uk";
114-
result = aggregateDomain(domain, static_suffix.size());
116+
result = convert(aggregateDomain(domain, static_suffix.size()));
115117
CHECK(result.first == "www.google.co.uk");
116118
CHECK(result.second == "");
117119
}

libs/visor_test/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
message(STATUS "Visor Lib Test Helper")
2+
3+
add_library(VisorLibTest INTERFACE)
4+
add_library(Visor::Lib::Test ALIAS VisorLibTest)
5+
6+
target_include_directories(VisorLibTest
7+
INTERFACE
8+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
9+
10+
target_link_libraries(VisorLibTest
11+
INTERFACE
12+
${CONAN_LIBS_SPDLOG}
13+
${CONAN_LIBS_CATCH2})
14+
15+
target_compile_features(VisorLibTest INTERFACE cxx_std_17)
16+
17+
target_sources(VisorLibTest INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/catch2/catch_test_visor.hpp)

0 commit comments

Comments
 (0)