-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
126 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,88 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#include "ebpfdiscovery/IpAddressChecker.h" | ||
#include <arpa/inet.h> | ||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
using namespace ebpfdiscovery; | ||
using namespace ::testing; | ||
|
||
class NetlinkCallsMock : public NetlinkCalls { | ||
public: | ||
MOCK_METHOD(int, sendIpAddrRequest, (int fd, sockaddr_nl* dst, int domain), (const, override)); | ||
MOCK_METHOD(int, sendBridgesRequest, (int fd, sockaddr_nl* dst, int domain), (const, override)); | ||
MOCK_METHOD(int, receive, (int fd, sockaddr_nl* dst, void* buf, size_t len), (const, override)); | ||
}; | ||
|
||
class IpAddressCheckerTest : public IpAddressChecker { | ||
protected: | ||
public: | ||
using IpAddressChecker::IpAddressChecker; | ||
IpAddressCheckerTest(std::initializer_list<IpIfce> config): IpAddressChecker(config) { | ||
IpAddressCheckerTest(std::initializer_list<IpIfce> config, const NetlinkCallsMock& mc) : IpAddressChecker(config, mc) { | ||
moveBridges(); | ||
} | ||
}; | ||
|
||
TEST(IpUtils, LocalBridgeIp) { | ||
IpAddressCheckerTest u({{{inet_addr("10.2.4.5")}, {}, 0x0000ffff, 0, true}, {{inet_addr("10.7.4.5")}, {}, 0x0000ffff, 0, false}}); | ||
TEST(IpAddressChecker, LocalBridgeIp) { | ||
const NetlinkCallsMock netlinkMock; | ||
IpAddressCheckerTest u( | ||
{{{inet_addr("10.2.4.5")}, {}, 0x0000ffff, 0, true}, {{inet_addr("10.7.4.5")}, {}, 0x0000ffff, 0, false}}, netlinkMock); | ||
EXPECT_FALSE(u.isAddressExternalLocal(inet_addr("10.2.6.5"))); | ||
} | ||
|
||
TEST(IpUtils, NOTLocalBridgeIp) { | ||
IpAddressCheckerTest u({{{inet_addr("10.2.6.5")}, {}, 0x0000ffff, 0, true}}); | ||
TEST(IpAddressChecker, NoReadIfSendFailed) { | ||
const NetlinkCallsMock netlinkMock; | ||
EXPECT_CALL(netlinkMock, sendIpAddrRequest).WillOnce(Return(-1)); | ||
EXPECT_CALL(netlinkMock, sendBridgesRequest).WillOnce(Return(-1)); | ||
IpAddressCheckerTest u({}, netlinkMock); | ||
u.readNetworks(); | ||
} | ||
|
||
TEST(IpAddressChecker, ReadAfterSuccessfulSend) { | ||
const NetlinkCallsMock netlinkMock; | ||
EXPECT_CALL(netlinkMock, sendIpAddrRequest).WillOnce(Return(1)); | ||
EXPECT_CALL(netlinkMock, sendBridgesRequest).WillOnce(Return(1)); | ||
EXPECT_CALL(netlinkMock, receive).Times(2).WillRepeatedly(Return(0)); | ||
IpAddressCheckerTest u({}, netlinkMock); | ||
u.readNetworks(); | ||
} | ||
|
||
TEST(IpAddressChecker, ReadUntilGreaterThan0) { | ||
const NetlinkCallsMock netlinkMock; | ||
EXPECT_CALL(netlinkMock, sendIpAddrRequest).WillOnce(Return(1)); | ||
EXPECT_CALL(netlinkMock, sendBridgesRequest).WillOnce(Return(1)); | ||
EXPECT_CALL(netlinkMock, receive).WillOnce(Return(1)).WillOnce(Return(0)).WillOnce(Return(1)).WillOnce(Return(0)); | ||
IpAddressCheckerTest u({}, netlinkMock); | ||
u.readNetworks(); | ||
} | ||
|
||
TEST(IpAddressChecker, NOTLocalBridgeIp) { | ||
const NetlinkCallsMock netlinkMock; | ||
IpAddressCheckerTest u({{{inet_addr("10.2.6.5")}, {}, 0x0000ffff, 0, true}}, netlinkMock); | ||
EXPECT_TRUE(u.isAddressExternalLocal(inet_addr("10.3.34.2"))); | ||
} | ||
|
||
TEST(IpUtils, SimpleClassATest) { | ||
TEST(IpAddressChecker, SimpleClassATest) { | ||
IpAddressCheckerTest u; | ||
EXPECT_TRUE(u.isAddressExternalLocal(inet_addr("192.168.1.2"))); | ||
} | ||
|
||
TEST(IpUtils, SimpleClassBTest) { | ||
TEST(IpAddressChecker, SimpleClassBTest) { | ||
IpAddressCheckerTest u; | ||
EXPECT_TRUE(u.isAddressExternalLocal(inet_addr("172.20.21.2"))); | ||
} | ||
|
||
TEST(IpUtils, SimpleClassCtest) { | ||
TEST(IpAddressChecker, SimpleClassCtest) { | ||
IpAddressCheckerTest u; | ||
EXPECT_TRUE(u.isAddressExternalLocal(inet_addr("10.2.4.5"))); | ||
} | ||
|
||
TEST(IpUtils, SimpleLinkLocal) { | ||
TEST(IpAddressChecker, SimpleLinkLocal) { | ||
IpAddressCheckerTest u; | ||
EXPECT_FALSE(u.isAddressExternalLocal(inet_addr("169.254.76.6"))); | ||
} | ||
|
||
|
||
TEST(IpUtils, SimplePublicIp) { | ||
TEST(IpAddressChecker, SimplePublicIp) { | ||
IpAddressCheckerTest u; | ||
EXPECT_FALSE(u.isAddressExternalLocal(inet_addr("170.254.76.6"))); | ||
} |