Skip to content
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

Added Common++ unit test project based on Gtest. #1507

Open
wants to merge 40 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
5f02028
Added Common++ unit test project based on Gtest.
Dimi1010 Jul 21, 2024
0687ddd
Fixed includes.
Dimi1010 Jul 21, 2024
a004e3c
Changed gtest version to 1.12 which is the last to support Cpp11.
Dimi1010 Jul 21, 2024
94a3b0e
Added Gmock in addition to Gtest.
Dimi1010 Jul 21, 2024
be20d78
Added IPv6 tests.
Dimi1010 Jul 21, 2024
3395ac7
Added IPAddress tests.
Dimi1010 Jul 21, 2024
55fdf10
Lint
Dimi1010 Jul 21, 2024
f447d9c
Fixed matcher error.
Dimi1010 Jul 21, 2024
5b37a1f
Added _ipv4 and _ipv6 string literals.
Dimi1010 Jul 21, 2024
12e4599
Added IPv4 Network unit tests.
Dimi1010 Jul 21, 2024
0f20bb9
Added IPv6 Network unit tests.
Dimi1010 Jul 21, 2024
a2718d8
Added IPNetwork unit tests.
Dimi1010 Jul 21, 2024
e29190a
Added more unit tests.
Dimi1010 Jul 22, 2024
0f3f927
Added IP(v4/v6)Address match network tests.
Dimi1010 Jul 22, 2024
c8d04de
Lint
Dimi1010 Jul 22, 2024
a67ebe7
Added MacAddress constructor from std::array.
Dimi1010 Jul 22, 2024
a93a202
Added MacAddress unit tests.
Dimi1010 Jul 22, 2024
66d1937
Added MacAddress tests for output to stream.
Dimi1010 Jul 22, 2024
aa92f72
Added unit tests for PointerVector and test fixture with built-in mem…
Dimi1010 Jul 22, 2024
d191cdb
Lint
Dimi1010 Jul 22, 2024
bd8ecd9
Included 'googletest' configuration for cppcheck.
Dimi1010 Jul 24, 2024
70708fd
Fixed include of MemoryLeakDetectorFixture... hopefully the correct way.
Dimi1010 Jul 24, 2024
d45b54d
Fixed wrong variable assert.
Dimi1010 Jul 24, 2024
a9cd1b8
Added unit test for functions in the general utilities header.
Dimi1010 Jul 24, 2024
4803471
Added unit tests for LRUList.
Dimi1010 Jul 24, 2024
9e77c18
Lint
Dimi1010 Jul 24, 2024
63a8fbe
Merge remote-tracking branch 'upstream/dev' into feature/gtest-framework
Dimi1010 Jul 30, 2024
d503bcd
Added cast to ptr type because direct nullptr is deleted function.
Dimi1010 Jul 30, 2024
bf9c3ca
Merge branch 'dev' into feature/gtest-framework
Dimi1010 Aug 15, 2024
f1baa83
Lint
Dimi1010 Aug 15, 2024
6b50130
Fixed issue with temporary.
Dimi1010 Aug 15, 2024
706fb4b
Merge branch 'dev' into feature/gtest-framework
Dimi1010 Sep 7, 2024
012df84
Lint - hopefully.
Dimi1010 Sep 7, 2024
e875c0b
Pulled operator << overloads into the 'pcpp' namespace to leverage AD…
Dimi1010 Sep 8, 2024
3a97325
Renamed MacAddress ToStream test to be consistent with IPAddress test…
Dimi1010 Sep 8, 2024
f07b0bf
Merge remote-tracking branch 'upstream/dev' into feature/gtest-framework
Dimi1010 Sep 8, 2024
01b5cc1
CMakeLists lint...
Dimi1010 Sep 8, 2024
ddf1d3a
Merge remote-tracking branch 'upstream/dev' into feature/gtest-framework
Dimi1010 Oct 24, 2024
c20e5d3
Merge remote-tracking branch 'upstream/dev' into feature/gtest-framework
Dimi1010 Nov 5, 2024
4b71919
Merge branch 'dev' into feature/gtest-framework
Dimi1010 Nov 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ repos:
args: ["--style=file", "-i"] # Use the .clang-format file for configuration and apply all fixes
files: ^(Common\+\+|Packet\+\+|Pcap\+\+|Tests|Examples)/.*\.(cpp|h)$
- id: cppcheck
args: ["--std=c++11", "--language=c++", "--suppressions-list=cppcheckSuppressions.txt", "--inline-suppr", "--force"]
args: ["--std=c++11", "--language=c++", "--suppressions-list=cppcheckSuppressions.txt", "--inline-suppr", "--force", "--library=googletest"]
- repo: https://github.com/BlankSpruce/gersemi
rev: 0.17.0
hooks:
Expand Down
75 changes: 44 additions & 31 deletions Common++/header/IpAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -1082,40 +1082,53 @@ namespace pcpp
std::unique_ptr<IPv4Network> m_IPv4Network;
std::unique_ptr<IPv6Network> m_IPv6Network;
};
} // namespace pcpp

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Address& ipv4Address)
{
os << ipv4Address.toString();
return os;
}
namespace literals
{
inline IPv4Address operator""_ipv4(const char* addrString, std::size_t size)
{
return IPv4Address(std::string(addrString, size));
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Address& ipv6Address)
{
os << ipv6Address.toString();
return os;
}
inline IPv6Address operator""_ipv6(const char* addrString, std::size_t size)
{
return IPv6Address(std::string(addrString, size));
}
} // namespace literals

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPAddress& ipAddress)
{
os << ipAddress.toString();
return os;
}
inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Address& ipv4Address)
{
os << ipv4Address.toString();
return os;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Network& network)
{
os << network.toString();
return os;
}
inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Address& ipv6Address)
{
os << ipv6Address.toString();
return os;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Network& network)
{
os << network.toString();
return os;
}
inline std::ostream& operator<<(std::ostream& os, const pcpp::IPAddress& ipAddress)
{
os << ipAddress.toString();
return os;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPNetwork& network)
{
os << network.toString();
return os;
}
inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Network& network)
{
os << network.toString();
return os;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Network& network)
{
os << network.toString();
return os;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPNetwork& network)
{
os << network.toString();
return os;
}
} // namespace pcpp
22 changes: 16 additions & 6 deletions Common++/header/MacAddress.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <array>
#include <algorithm>
#include <initializer_list>
#include <iterator>
Expand Down Expand Up @@ -41,6 +42,15 @@ namespace pcpp
memcpy(m_Address, addr, sizeof(m_Address));
}

/**
* A constructor that creates an instance of the class out of a std::array.
* @param[in] addr The std::array containing 6 bytes representing the MAC address.
*/
explicit MacAddress(std::array<uint8_t, 6> const& addr)
{
std::copy(addr.begin(), addr.end(), std::begin(m_Address));
}

/**
* A constructor that creates an instance of the class out of a std::string.
* If the string doesn't represent a valid MAC address, the constructor throws an exception.
Expand Down Expand Up @@ -173,10 +183,10 @@ namespace pcpp
private:
uint8_t m_Address[6] = { 0 };
};
} // namespace pcpp

inline std::ostream& operator<<(std::ostream& os, const pcpp::MacAddress& macAddress)
{
os << macAddress.toString();
return os;
}
inline std::ostream& operator<<(std::ostream& os, const pcpp::MacAddress& macAddress)
{
os << macAddress.toString();
return os;
}
} // namespace pcpp
1 change: 1 addition & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
if(PCAPPP_BUILD_TESTS)
add_subdirectory(Common++Test)
add_subdirectory(Packet++Test)
add_subdirectory(Pcap++Test)
add_subdirectory(PcppTestFramework)
Expand Down
51 changes: 51 additions & 0 deletions Tests/Common++Test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
cmake_minimum_required(VERSION 3.14)

include(FetchContent)

FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.0)

if(WIN32)
# Prevent overriding the parent project's compiler/linker settings.
set(gtest_force_shared_crt
ON
CACHE BOOL "" FORCE)
endif()

FetchContent_MakeAvailable(googletest)

add_executable(
Common++Test
Tests/GeneralUtilsTests.cpp
Tests/IPAddressTests.cpp
Tests/LRUListTests.cpp
Tests/MacAddressTests.cpp
Tests/PointerVectorTests.cpp)

target_link_libraries(
Common++Test
PRIVATE Common++
memplumber
gtest
gmock
gmock_main)

target_include_directories(Common++Test PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/Utils>)

if(MSVC)
# This executable requires getopt.h not available on VStudio
target_link_libraries(Common++Test PRIVATE Getopt-for-Visual-Studio)
endif()

set_property(TARGET Common++Test PROPERTY RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/Bin")
set_property(TARGET Common++Test PROPERTY RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/Bin")
set_property(TARGET Common++Test PROPERTY RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/Bin")

enable_testing()

add_test(
NAME Common++Test
COMMAND $<TARGET_FILE:Common++Test>
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/)
38 changes: 38 additions & 0 deletions Tests/Common++Test/Tests/GeneralUtilsTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <array>
#include <cstring>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include "GeneralUtils.h"

namespace pcpp
{
TEST(GeneralUtilsTests, byteArrayToHexString)
{
std::array<uint8_t, 3> byteArr = { 0xaa, 0x2b, 0x10 };
EXPECT_EQ(byteArrayToHexString(byteArr.data(), byteArr.size()), "aa2b10");
};

TEST(GeneralUtilsTests, hexStringToByteArray)
{
std::array<uint8_t, 3> resultByteArr = { 0 };
EXPECT_EQ(hexStringToByteArray("aa2b10", resultByteArr.data(), resultByteArr.size()), 3);
EXPECT_EQ(resultByteArr, (std::array<uint8_t, 3>{ 0xaa, 0x2b, 0x10 }));
};

TEST(GeneralUtilsTests, cross_platform_memmem)
{
const char haystack[] = "Hello, World!";
const char needle[] = "World";
EXPECT_EQ(cross_platform_memmem(haystack, sizeof(haystack), needle,
sizeof(needle) - 1 /* ignore the null terminator */),
haystack + 7);
};

TEST(GeneralUtilsTests, align)
{
EXPECT_EQ(align<4>(3), 4);
EXPECT_EQ(align<4>(4), 4);
EXPECT_EQ(align<4>(5), 8);
};
} // namespace pcpp
Loading
Loading