test: Mock out interface lookups
This will make it possible to do interface lookups inside of our classes
during testing without depending on real interfaces existing.
Change-Id: I4c273d6961fa4229401fb25a0e5eb06af5b59ca4
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/test_neighbor.cpp b/test/test_neighbor.cpp
index 6491b9f..ffb0d5d 100644
--- a/test/test_neighbor.cpp
+++ b/test/test_neighbor.cpp
@@ -1,3 +1,4 @@
+#include "mock_syscall.hpp"
#include "neighbor.hpp"
#include "util.hpp"
@@ -5,7 +6,6 @@
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/ethernet.h>
-#include <net/if.h>
#include <cstring>
#include <stdexcept>
@@ -22,6 +22,9 @@
namespace detail
{
+constexpr auto ifStr = "eth0";
+constexpr auto ifIdx = 1;
+
TEST(ParseNeighbor, NotNeighborType)
{
nlmsghdr hdr{};
@@ -58,10 +61,12 @@
TEST(ParseNeighbor, NoAttrs)
{
+ mock_addIF(ifStr, ifIdx);
+
nlmsghdr hdr{};
hdr.nlmsg_type = RTM_NEWNEIGH;
ndmsg msg{};
- msg.ndm_ifindex = if_nametoindex("lo");
+ msg.ndm_ifindex = ifIdx;
ASSERT_NE(0, msg.ndm_ifindex);
std::string data;
data.append(reinterpret_cast<char*>(&msg), sizeof(msg));
@@ -73,10 +78,12 @@
TEST(ParseNeighbor, NoAddress)
{
+ mock_addIF(ifStr, ifIdx);
+
nlmsghdr hdr{};
hdr.nlmsg_type = RTM_NEWNEIGH;
ndmsg msg{};
- msg.ndm_ifindex = if_nametoindex("lo");
+ msg.ndm_ifindex = ifIdx;
ASSERT_NE(0, msg.ndm_ifindex);
ether_addr mac = {{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}};
rtattr lladdr{};
@@ -97,13 +104,14 @@
TEST(ParseNeighbor, NoMAC)
{
- constexpr auto ifstr = "lo";
+ mock_addIF(ifStr, ifIdx);
+
nlmsghdr hdr{};
hdr.nlmsg_type = RTM_NEWNEIGH;
ndmsg msg{};
msg.ndm_family = AF_INET;
msg.ndm_state = NUD_PERMANENT;
- msg.ndm_ifindex = if_nametoindex(ifstr);
+ msg.ndm_ifindex = ifIdx;
ASSERT_NE(0, msg.ndm_ifindex);
in_addr addr;
ASSERT_EQ(1, inet_pton(msg.ndm_family, "192.168.10.1", &addr));
@@ -121,7 +129,7 @@
std::vector<NeighborInfo> neighbors;
parseNeighbor(hdr, data, neighbors);
EXPECT_EQ(1, neighbors.size());
- EXPECT_EQ(ifstr, neighbors[0].interface);
+ EXPECT_EQ(ifStr, neighbors[0].interface);
EXPECT_TRUE(neighbors[0].permanent);
EXPECT_FALSE(neighbors[0].mac);
EXPECT_TRUE(equal(addr, std::get<in_addr>(neighbors[0].address)));
@@ -129,13 +137,14 @@
TEST(ParseNeighbor, Full)
{
- constexpr auto ifstr = "lo";
+ mock_addIF(ifStr, ifIdx);
+
nlmsghdr hdr{};
hdr.nlmsg_type = RTM_NEWNEIGH;
ndmsg msg{};
msg.ndm_family = AF_INET6;
msg.ndm_state = NUD_NOARP;
- msg.ndm_ifindex = if_nametoindex(ifstr);
+ msg.ndm_ifindex = ifIdx;
ether_addr mac = {{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}};
rtattr lladdr{};
lladdr.rta_len = RTA_LENGTH(sizeof(mac));
@@ -161,7 +170,7 @@
std::vector<NeighborInfo> neighbors;
parseNeighbor(hdr, data, neighbors);
EXPECT_EQ(1, neighbors.size());
- EXPECT_EQ(ifstr, neighbors[0].interface);
+ EXPECT_EQ(ifStr, neighbors[0].interface);
EXPECT_FALSE(neighbors[0].permanent);
EXPECT_TRUE(neighbors[0].mac);
EXPECT_TRUE(equal(mac, *neighbors[0].mac));