-
Notifications
You must be signed in to change notification settings - Fork 721
Added Common++ unit test project based on Gtest. #1507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
- Added IPAddress multicast unit tests. - Added IPNetwork includes unit tests. - Formatting
…es from google test.
@seladb @egecetin @tigercosmos @clementperon So... minor issue with the google test integration. The framework allocates to heap during unit tests. I don't think we can run the leak tracking on per test level as is. It might be workable on a global level though. I am open to suggestions on how to proceed. |
I am okay to move the memory-leaking test to global as long as it helps to find out issues. |
Update on the memory leak infrastructure: It still doesn't work directly on global level as gtest uses thread local variables that end up lazy initializing at runtime, but get released on program exit. :/ |
Not sure if I fully understood. Aren't all tests passed now? |
Tests pass because the memleak is disabled in the gtest project currently. [1] |
@tigercosmos In my tests, it didn't work having the memory tracking encompass all the tests as a single start/stop pair, either. That is because gtest appears to allocate on heap from thread local variables during the tests. That memory isn't released until the main thread actually exits when the program shuts down, and gets detected as a false positive. :/ (and leads to crash when the thread actually tries to deallocate) This is the culprit. From googletest v1.16.0 gtest-port.h
|
include(FetchContent) | ||
|
||
# TODO: This may have issues with brew as it doesn't allow the use of FetchContent. | ||
FetchContent_Declare(googletest GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG v1.16.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the TODO
comment mentions, package managers don't approve packages that use FetchContent_Declare
, we already had this experience before...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. Tbh, we could possibly have a copy of the lib in 3rdParty
to build the tests with.
I am unsure if we could use the brew version since we need to use up to 1.16 as 1.17 (current newest) requires cpp17.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, putting it in 3rdParty
is the only option to keep PcapPlusPlus in package managers.
Though I'm still not sure that we want to merge this PR. Having 2 test frameworks doesn't look like a good idea, and converting all of our tests to Gtest will be an extremely hard task
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did start another branch [1] off of this one, for the Packet++ tests, to see how it would go. You can take a look, if you want. The new project is Tests/Packet++Test-google
.
It actually mostly involves trivial changes, like macro replacements.
PTF_TEST_CASE
->TEST(Fixture, TestName)
PTF_ASSERT_EQUAL
->ASSERT_EQ
/EXPECT_EQ
PTF_ASSERT_NULL
->ASSERT_EQ(statement, nullptr)
PTF_ASSERT_NOT_NULL
->ASSERT_NE(statement, nullptr)
The only non-trivial assertion so far has been PTF_ASSERT_BUF_COMPARE
which was replaced by EXPECT_TRUE(test::BuffersMatch(actualBuf, actualBufSize, expectedBuf, expectedBufSize))
which ended up being a more comprehensive check IMO. [2].
The loading utilities needed a revamp, as I didn't want to use macros everywhere and the current ones had a limitation of always using the current working directory (has issues with out of source builds).
For the majority of the cases the code:
timeval time;
gettimeofday(&time, nullptr);
READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/TcpPacketNoOptions.dat");
// rest of test case
ended up being a one liner:
// std::unique_ptr<RawPacket>
auto rawPacket1 = test::createPacketFromHexResource("PacketExamples/TcpPacketWithOptions3.dat", /* optional packet factory */, /* optional data loader */);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how we'll be able to review these huge PRs...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, that makes sense. I just wanted to confirm that we're not missing anything.
So I guess we decided to go with Gtest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seladb I guess so.
IMO, the first thing to do would be to disable building the tests in vcpkg
and homebrew
install scenarios, via a revision to the portfile / bottle, so we know it can work fine as is, before adding fetch content to the test infrastructure.
Unless anyone has any objections? @egecetin @clementperon @tigercosmos
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I think that's a good idea 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened a PR to disable the tests in vcpkg
here microsoft/vcpkg#46941.
Also found a bug when examining the build. It placed the test executables inside the vcpkg_installed/vcpkg/blds/pcapplusplus/src/***.clean
which is a bug, IMO.
Our test projects don't really support out of source builds currently, so I plan on fixing this in the Gtest rework.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Homebrew has also been updated Homebrew/homebrew-core#233719
Conan needs no update as we already don't build tests there on install.
# Conflicts: # .pre-commit-config.yaml
# Conflicts: # Tests/CMakeLists.txt
This reverts commit 789a17e.
Summary
This PR introduces dedicated unit tests for the Common++ module based on the GoogleTest framework as well as several minor improvements.
What's Changed?
Unit tests
Test/Common++
has been added with unit tests.IPAddress
,IPv4Address
,IPv6Address
- tests provided inIPAddressTests.cpp
IPNetwork
,IPv4Network
,IPv6Network
- tests provided inIPAddressTests.cpp
MacAddress
- tests provided inMacAddressTests.cpp
PointereVector<T>
- tests provided inPointerVectorTests.cpp
LRUList<T>
- tests provided inLRUListTests.cpp
byteArrayToHexString
- tests provided inGeneralUtilsTests.cpp
hexStringToByteArray
- tests provided inGeneralUtilsTests.cpp
cross_platform_memmem
- tests provided inGeneralUtilsTests.cpp
align
- tests provided inGeneralUtilsTests.cpp
googletest
(v1.16.0) during the cmake build process.googletest
macro definitions for pre-commit'scppcheck
step.Fixes and additions
MacAddress
that takes anstd::array<uint8_t, 6>
.pcpp::literals
containing literals for quickly constructingIPv4Address
(_ipv4
) andIPv6Address
(_ipv6
).std::ostream
operator<<
overloads for inside thepcpp
namespace to fix ADL resolution issues for the following classes:IPAddress
IPv4Address
IPv6Address
IPNetwork
IPv4Network
IPv6Network
MacAddress