-
Notifications
You must be signed in to change notification settings - Fork 722
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
|
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_EQPTF_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 caseended 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.
…nally fetching it from FetchContent.
|
@seladb Updated the PR description, so you might want to reread that. Main update is |
|
|
||
| #include "GeneralUtils.h" | ||
|
|
||
| namespace pcpp |
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.
Should probably change the namespace to pcpp_tests...
Summary
This PR introduces dedicated unit tests for the Common++ module based on the GoogleTest framework as well as several minor improvements. The dependency is resolved via
find_packageand conditionally viaFetchContentiffind_packagefails.What's Changed?
New dependency - GoogleTest
Added GoogleTest as dependency to the project when
PCAPPP_BUILD_TESTSis set toON. The dependency resolution happens in one of two ways:find_packageorFetchContent. The project prioritizes the result offind_package, to allow environments to supply their own.Restricted FetchContent dependency resolution.
For environments that disallow or discourage the use of
FetchContent(e.g. brew, vcpkg, etc) an additional project optionPCAPPP_ALLOW_FETCH_CONTENT(default:ON) is added. Setting the option toOFFwould disable the automatic fetching and require all dependencies that are optionally supplied by fetch content be resolvable byfind_packagein the build environment.NB: The
googletestdependency is still declared inside theFetchContentinfrastructure, but the option disables callingFetchContent_MakeAvailableeffectively disabling the fetch operation.New Unit tests
Test/Common++has been added with unit tests.IPAddress,IPv4Address,IPv6Address- tests provided inIPAddressTests.cppIPNetwork,IPv4Network,IPv6Network- tests provided inIPAddressTests.cppMacAddress- tests provided inMacAddressTests.cppPointereVector<T>- tests provided inPointerVectorTests.cppLRUList<T>- tests provided inLRUListTests.cppbyteArrayToHexString- tests provided inGeneralUtilsTests.cpphexStringToByteArray- tests provided inGeneralUtilsTests.cppcross_platform_memmem- tests provided inGeneralUtilsTests.cppalign- tests provided inGeneralUtilsTests.cppgoogletest(v1.16.0) during the cmake build process.googletestmacro definitions for pre-commit'scppcheckstep.Fixes and additions
#include <array>toMacAddress.hto fix transitive include.pcpp::literalscontaining literals for quickly constructingIPv4Address(_ipv4) andIPv6Address(_ipv6).