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 7 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
5 changes: 3 additions & 2 deletions Tests/Common++Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.11.0
GIT_TAG release-1.12.0
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved
)

if(WIN32)
Expand All @@ -24,7 +24,8 @@ target_link_libraries(
Common++Test
PRIVATE Common++
gtest
gtest_main
gmock
gmock_main
)

if(MSVC)
Expand Down
175 changes: 161 additions & 14 deletions Tests/Common++Test/Tests/IPAddressTests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <array>
#include <cstring>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include "IpAddress.h"

Expand All @@ -22,7 +24,7 @@ namespace pcpp
EXPECT_TRUE(IPv4Address::isValidIPv4Address("222.146.254.245"));
EXPECT_FALSE(IPv4Address::isValidIPv4Address("222.146.300.245"));
EXPECT_FALSE(IPv4Address::isValidIPv4Address("bogus string"));
}
};

TEST(IPv4AddressTest, IPv4AddressBasics)
{
Expand All @@ -34,14 +36,16 @@ namespace pcpp
IPv4Address ipString("0.0.0.1");
EXPECT_EQ(ipString.toInt(), 0x01000000);
EXPECT_EQ(ipString.toString(), "0.0.0.1");
EXPECT_THROW(IPv4Address("0.0.0.644"), std::invalid_argument) << "IPv4Address does not throw for out of bounds IP string.";
EXPECT_THROW(IPv4Address("bogusString"), std::invalid_argument) << "IPv4Address does not throw for non-IP string.";
EXPECT_THROW(IPv4Address("0.0.0.644"), std::invalid_argument)
<< "IPv4Address does not throw for out of bounds IP string.";
EXPECT_THROW(IPv4Address("bogusString"), std::invalid_argument)
<< "IPv4Address does not throw for non-IP string.";

IPv4Address ipUint32(0x085201A0);
EXPECT_EQ(ipUint32.toInt(), 0x085201A0);
EXPECT_EQ(ipUint32.toString(), "160.1.82.8");

std::array<uint8_t, 4> ipArrayBuffer = {192, 100, 1, 1};
std::array<uint8_t, 4> ipArrayBuffer = { 192, 100, 1, 1 };
IPv4Address ipUint8Raw(ipArrayBuffer.data());
EXPECT_EQ(ipUint8Raw.toInt(), 0x010164C0);
EXPECT_EQ(ipUint8Raw.toString(), "192.100.1.1");
Expand All @@ -54,37 +58,180 @@ namespace pcpp
EXPECT_EQ(ipUint8Array.toByteArray(), ipArrayBuffer);
EXPECT_TRUE(0 == std::memcmp(ipArrayBuffer.data(), ipUint8Array.toBytes(), 4));

EXPECT_EQ(ipUint8Raw, ipUint8Array) << "Comparison operator '==' does not compare equal values.";
EXPECT_NE(ipUint8Raw, ipDefault) << "Comparison operator '!=' does not compare unequal values.";
EXPECT_TRUE(ipUint8Raw == ipUint8Array) << "Comparison operator '==' does not compare equal values correctly.";
EXPECT_FALSE(ipUint8Raw == ipDefault) << "Comparison operator '==' does not compare unequal values correctly.";
EXPECT_FALSE(ipUint8Raw != ipUint8Array) << "Comparison operator '!=' does not compare equal values correctly.";
EXPECT_TRUE(ipUint8Raw != ipDefault) << "Comparison operator '!=' does not compare unequal values correctly.";

EXPECT_TRUE(ipDefault < ipString) << "Comparison operator '<' does not compare less than values correctly.";
EXPECT_FALSE(ipString < ipDefault) << "Comparison operator '<' does not compare less than values correctly.";
};

TEST(IPv4AddressTest, Multicast)
{
IPv4Address underMulticastBound(0x000000D1);
EXPECT_FALSE(underMulticastBound.isMulticast());

IPv4Address atLowerMulticastBound(0x000000E0);
EXPECT_TRUE(atLowerMulticastBound.isMulticast());

IPv4Address inMulticastRange(0x000000EF);
EXPECT_TRUE(inMulticastRange.isMulticast());

IPv4Address atUpperMulticastBound(0xFFFFFFEF);
EXPECT_TRUE(atUpperMulticastBound.isMulticast());

IPv4Address overMulticastBound(0x000000F0);
EXPECT_FALSE(overMulticastBound.isMulticast());
};

TEST(IPv4AddressTest, MatchNetwork)
{
FAIL() << "Not Implemented";
}
};

TEST(IPv6AddressTest, IPv6AddressStatics)
{
IPv6Address const& ipZero = IPv6Address::Zero;
EXPECT_EQ(ipZero.toString(), "::");
EXPECT_THAT(ipZero.toByteArray(), ::testing::Each(0));

IPv6Address const& ipMulticastLower = IPv6Address::MulticastRangeLowerBound;
EXPECT_EQ(ipMulticastLower.toString(), "ff00::");
EXPECT_THAT(ipMulticastLower.toByteArray(),
::testing::ElementsAre(0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00));
};

TEST(IPv6AddressTest, IPv6AddressBasics)
{
IPv6Address ipDefault;
EXPECT_EQ(ipDefault.toString(), "::");
EXPECT_THAT(ipDefault.toByteArray(), ::testing::Each(0));

IPv6Address ipString("2001:0db8:85a3:0000:0000:8a4e:0370:7334");
EXPECT_EQ(ipString.toString(), "2001:db8:85a3::8a4e:370:7334");
EXPECT_THAT(ipString.toByteArray(), ::testing::ElementsAre(0x20, 0x01, 0x0D, 0xB8, 0x85, 0xA3, 0x00, 0x00, 0x00,
0x00, 0x8A, 0x4E, 0x03, 0x70, 0x73, 0x34));

EXPECT_THROW(IPv6Address("2001:0db8:85a3:0000:0000:8a4e:0370:7334:extra"), std::invalid_argument)
<< "IPv6Address does not throw for out of bounds IP string.";
EXPECT_THROW(IPv6Address("2001::ab01::c"), std::invalid_argument)
<< "IPv6Address does not throw for multiple double colon in IP string.";
EXPECT_THROW(IPv6Address("bogusString"), std::invalid_argument)
<< "IPv6Address does not throw for non-IP string.";

std::array<uint8_t, 16> ipArrayBuffer = { 0x20, 0x01, 0x0D, 0xB8, 0x85, 0xA3, 0x00, 0x00,
0x00, 0x00, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x34 };

IPv6Address ipUint8Raw(ipArrayBuffer.data());
EXPECT_EQ(ipUint8Raw.toString(), "2001:db8:85a3::8a2e:370:7334");
EXPECT_THAT(ipUint8Raw.toByteArray(), ::testing::ElementsAre(0x20, 0x01, 0x0D, 0xB8, 0x85, 0xA3, 0x00, 0x00,
0x00, 0x00, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x34));

IPv6Address ipUint8Array(ipArrayBuffer);
EXPECT_EQ(ipUint8Array.toString(), "2001:db8:85a3::8a2e:370:7334");
EXPECT_THAT(ipUint8Array.toByteArray(), ::testing::ElementsAre(0x20, 0x01, 0x0D, 0xB8, 0x85, 0xA3, 0x00, 0x00,
0x00, 0x00, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x34));

EXPECT_TRUE(ipUint8Raw == ipUint8Array) << "Comparison operator '==' does not compare equal values correctly.";
EXPECT_FALSE(ipUint8Raw == ipDefault) << "Comparison operator '==' does not compare unequal values correctly.";
EXPECT_FALSE(ipUint8Raw != ipUint8Array) << "Comparison operator '!=' does not compare equal values correctly.";
EXPECT_TRUE(ipUint8Raw != ipDefault) << "Comparison operator '!=' does not compare unequal values correctly.";

EXPECT_TRUE(ipDefault < ipString) << "Comparison operator '<' does not compare less than values correctly.";
EXPECT_FALSE(ipString < ipDefault) << "Comparison operator '<' does not compare less than values correctly.";

std::array<uint8_t, 16> outBuffer = {};
ipUint8Array.copyTo(outBuffer.data());
EXPECT_EQ(ipUint8Array.toByteArray(), outBuffer);

uint8_t* heapOutBuffer = nullptr;
std::size_t heapOutBufferSize = 0;
ipUint8Array.copyTo(&heapOutBuffer, heapOutBufferSize);

ASSERT_NE(heapOutBuffer, nullptr);
EXPECT_EQ(heapOutBufferSize, 16);
EXPECT_TRUE(0 == std::memcmp(ipArrayBuffer.data(), heapOutBuffer, 16));
delete[] heapOutBuffer;
};

TEST(IPv6AddressTest, Multicast)
{
IPv6Address underMulticastBound("fef0::");
EXPECT_FALSE(underMulticastBound.isMulticast());

IPv6Address atLowerMulticastBound("ff00::");
EXPECT_TRUE(atLowerMulticastBound.isMulticast());

TEST(IPv6AddressTest, IPv6AddressTest)
IPv6Address inMulticastRange("ff00::ef");
EXPECT_TRUE(inMulticastRange.isMulticast());
};

TEST(IPv6AddressTest, MatchNetwork)
{
FAIL() << "Not Implemented";
};

TEST(IPAddressTest, IPAddressTest)
TEST(IPAddressTest, IPAddressBasics)
{
IPAddress ipDefault;
EXPECT_EQ(ipDefault.getType(), IPAddress::AddressType::IPv4AddressType);
EXPECT_EQ(ipDefault.getIPv4(), IPv4Address::Zero);
EXPECT_TRUE(ipDefault.isZero());
EXPECT_EQ(ipDefault.toString(), "0.0.0.0");

IPAddress ip4String("192.168.0.1");
EXPECT_EQ(ip4String.getType(), IPAddress::AddressType::IPv4AddressType);
EXPECT_EQ(ip4String.getIPv4(), IPv4Address("192.168.0.1"));
EXPECT_FALSE(ip4String.isZero());
EXPECT_EQ(ip4String.toString(), "192.168.0.1");

IPAddress ip6ZeroString("::");
EXPECT_EQ(ip6ZeroString.getType(), IPAddress::AddressType::IPv6AddressType);
EXPECT_EQ(ip6ZeroString.getIPv6(), IPv6Address::Zero);
EXPECT_TRUE(ip6ZeroString.isZero());
EXPECT_EQ(ip6ZeroString.toString(), "::");

IPAddress ip6String("2001:db8:85a3::8a2e:370:7334");
EXPECT_EQ(ip6String.getType(), IPAddress::AddressType::IPv6AddressType);
EXPECT_EQ(ip6String.getIPv6(), IPv6Address("2001:db8:85a3::8a2e:370:7334"));
EXPECT_FALSE(ip6String.isZero());
EXPECT_EQ(ip6String.toString(), "2001:db8:85a3::8a2e:370:7334");

EXPECT_THROW(IPAddress("192.168.300.1"), std::invalid_argument);
EXPECT_THROW(IPAddress("2001:db8:85a3::8a2e:370:7334:extra"), std::invalid_argument)
<< "IPAddress does not throw for out of bounds IP string.";
EXPECT_THROW(IPv6Address("2001::ab01::c"), std::invalid_argument)
<< "IPAddress does not throw for multiple double colon in IP string.";
EXPECT_THROW(IPAddress("bogusString"), std::invalid_argument) << "IPAddress does not throw for non-IP string.";

EXPECT_TRUE(ipDefault == IPv4Address::Zero) << "Comparison operator '==' does not compare equal values correctly.";
EXPECT_FALSE(ipDefault != IPv4Address::Zero) << "Comparison operator '!=' does not compare equal values correctly.";

EXPECT_FALSE(ipDefault == ip6ZeroString) << "Comparison operator '==' between IPv4 and IPv6 should always return false";
EXPECT_TRUE(ipDefault != ip6ZeroString) << "Comparison operator '!=' between IPv4 and IPv6 should always return true";

// Todo: less than operator
};

TEST(IPAddressTest, Multicast)
{
FAIL() << "Not Implemented";
}
};

TEST(IPv4NetworkTest, IPv4NetworkTest)
{
FAIL() << "Not Implemented";
}
};

TEST(IPv6NetworkTest, IPv6NetworkTest)
{
FAIL() << "Not Implemented";
}
};

TEST(IPNetworkTest, IPNetworkTest)
{
FAIL() << "Not Implemented";
}
} // namespace pcpp
};
} // namespace pcpp
Loading