test: Make mock syscalls take InterfaceInfo

This will make it easier to expand the parameters and plumb data through
the mock.

Change-Id: I5ca015a0494c092d9d247bbb2b7d0122c5e58fd8
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/mock_syscall.cpp b/test/mock_syscall.cpp
index e481afd..bf68d47 100644
--- a/test/mock_syscall.cpp
+++ b/test/mock_syscall.cpp
@@ -1,4 +1,5 @@
-#include "system_queries.hpp"
+#include "mock_syscall.hpp"
+
 #include "util.hpp"
 
 #include <arpa/inet.h>
@@ -31,26 +32,28 @@
 std::map<std::string, InterfaceInfo> mock_if;
 std::map<int, std::string> mock_if_indextoname;
 
-void mock_clear()
+void phosphor::network::system::mock_clear()
 {
     mock_rtnetlinks.clear();
     mock_if.clear();
     mock_if_indextoname.clear();
 }
 
-void mock_addIF(const std::string& name, unsigned idx, unsigned flags,
-                std::optional<ether_addr> mac, std::optional<unsigned> mtu)
+void phosphor::network::system::mock_addIF(const InterfaceInfo& info)
 {
-    if (idx == 0)
+    if (info.idx == 0)
     {
         throw std::invalid_argument("Bad interface index");
     }
-
-    mock_if.emplace(
-        name,
-        InterfaceInfo{
-            .idx = idx, .flags = flags, .name = name, .mac = mac, .mtu = mtu});
-    mock_if_indextoname.emplace(idx, name);
+    for (const auto& [_, iinfo] : mock_if)
+    {
+        if (iinfo.idx == info.idx || iinfo.name == info.name)
+        {
+            throw std::invalid_argument("Interface already exists");
+        }
+    }
+    mock_if.emplace(info.name.value(), info);
+    mock_if_indextoname.emplace(info.idx, info.name.value());
 }
 
 void validateMsgHdr(const struct msghdr* msg)
diff --git a/test/mock_syscall.hpp b/test/mock_syscall.hpp
index 39c3986..4b0cb20 100644
--- a/test/mock_syscall.hpp
+++ b/test/mock_syscall.hpp
@@ -1,19 +1,11 @@
 #pragma once
-#include <net/ethernet.h>
+#include "system_queries.hpp"
 
-#include <optional>
-#include <string>
-
-/** @brief Clears out the interfaces and IPs configured for mocking
- */
+namespace phosphor::network::system
+{
+/** @brief Clears out the interfaces and IPs configured for mocking */
 void mock_clear();
 
-/** @brief Adds an address string to index mapping and MAC mapping
- *
- *  @param[in] name - Interface name
- *  @param[in] idx  - Interface index
- *  @param[in] mac  - Interface MAC address
- */
-void mock_addIF(const std::string& name, unsigned idx, unsigned flags = 0,
-                std::optional<ether_addr> mac = std::nullopt,
-                std::optional<unsigned> mtu = std::nullopt);
+/** @brief Adds an interface definition to the mock system */
+void mock_addIF(const InterfaceInfo& info);
+} // namespace phosphor::network::system
diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp
index e65d03c..654f45d 100644
--- a/test/test_ethernet_interface.cpp
+++ b/test/test_ethernet_interface.cpp
@@ -44,11 +44,11 @@
     static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus,
                                                MockManager& manager)
     {
-        mock_clear();
-        mock_addIF("test0", /*idx=*/1);
-        return {bus, manager,
-                system::InterfaceInfo{.idx = 1, .flags = 0, .name = "test0"},
-                "/xyz/openbmc_test/network"sv, config::Parser()};
+        system::mock_clear();
+        system::InterfaceInfo info{.idx = 1, .flags = 0, .name = "test0"};
+        system::mock_addIF(info);
+        return {bus, manager, info, "/xyz/openbmc_test/network"sv,
+                config::Parser()};
     }
 
     int countIPObjects()
@@ -110,17 +110,16 @@
     EXPECT_EQ("", interface.macAddress());
     EXPECT_FALSE(interface.linkUp());
 
-    constexpr unsigned idx = 2;
     constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
     constexpr unsigned mtu = 150;
 
-    mock_addIF("test1", idx, IFF_RUNNING, mac, mtu);
-    MockEthernetInterface intf(bus, manager,
-                               system::InterfaceInfo{.idx = idx,
-                                                     .flags = IFF_RUNNING,
-                                                     .name = "test1",
-                                                     .mac = mac,
-                                                     .mtu = mtu},
+    system::InterfaceInfo info{.idx = 2,
+                               .flags = IFF_RUNNING,
+                               .name = "test1",
+                               .mac = mac,
+                               .mtu = mtu};
+    system::mock_addIF(info);
+    MockEthernetInterface intf(bus, manager, info,
                                "/xyz/openbmc_test/network"sv, config::Parser());
 
     EXPECT_EQ(mtu, intf.mtu());
diff --git a/test/test_netlink.cpp b/test/test_netlink.cpp
index 71be8e9..c5292a7 100644
--- a/test/test_netlink.cpp
+++ b/test/test_netlink.cpp
@@ -293,12 +293,13 @@
 class PerformRequest : public testing::Test
 {
   public:
-    void doLinkDump(size_t ifs)
+    void doLinkDump(unsigned ifs)
     {
-        mock_clear();
-        for (size_t i = 0; i < ifs; ++i)
+        system::mock_clear();
+        for (unsigned i = 0; i < ifs; ++i)
         {
-            mock_addIF(fmt::format("eth{}", i), /*idx=*/1 + i);
+            system::mock_addIF(system::InterfaceInfo{
+                .idx = i + 1u, .flags = 0, .name = fmt::format("eth{}", i)});
         }
 
         size_t cbCalls = 0;
diff --git a/test/test_network_manager.cpp b/test/test_network_manager.cpp
index 5298719..459bacf 100644
--- a/test/test_network_manager.cpp
+++ b/test/test_network_manager.cpp
@@ -28,6 +28,7 @@
         bus(sdbusplus::bus::new_default()),
         manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir())
     {
+        system::mock_clear();
     }
 
     void createInterfaces()
@@ -39,17 +40,14 @@
 // getifaddrs will not return any interface
 TEST_F(TestNetworkManager, NoInterface)
 {
-    mock_clear();
     createInterfaces();
     EXPECT_TRUE(manager.getInterfaces().empty());
 }
 // getifaddrs returns single interface.
 TEST_F(TestNetworkManager, WithSingleInterface)
 {
-    mock_clear();
-
     // Adds the following ip in the getifaddrs list.
-    mock_addIF("igb1", /*idx=*/2);
+    system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"});
 
     // Now create the interfaces which will call the mocked getifaddrs
     // which returns the above interface detail.
@@ -60,10 +58,8 @@
 // getifaddrs returns two interfaces.
 TEST_F(TestNetworkManager, WithMultipleInterfaces)
 {
-    mock_clear();
-
-    mock_addIF("igb0", /*idx=*/1);
-    mock_addIF("igb1", /*idx=*/2);
+    system::mock_addIF({.idx = 1, .flags = 0, .name = "igb0"});
+    system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"});
 
     createInterfaces();
     EXPECT_THAT(manager.getInterfaces(),
diff --git a/test/test_vlan_interface.cpp b/test/test_vlan_interface.cpp
index c17bf97..d5b6989 100644
--- a/test/test_vlan_interface.cpp
+++ b/test/test_vlan_interface.cpp
@@ -41,11 +41,12 @@
     static EthernetInterface makeInterface(sdbusplus::bus_t& bus,
                                            MockManager& manager)
     {
-        mock_clear();
-        mock_addIF("test0", /*idx=*/1);
+        system::mock_clear();
+        system::InterfaceInfo info{.idx = 1, .flags = 0, .name = "test0"};
+        system::mock_addIF(info);
         return {bus,
                 manager,
-                system::InterfaceInfo{.idx = 1, .flags = 0, .name = "test0"},
+                info,
                 "/xyz/openbmc_test/network"sv,
                 config::Parser(),
                 /*emitSignal=*/false,
@@ -54,9 +55,8 @@
 
     void createVlan(uint16_t id)
     {
-        std::string ifname = "test0.";
-        ifname += std::to_string(id);
-        mock_addIF(ifname.c_str(), 1000 + id);
+        system::mock_addIF(system::InterfaceInfo{
+            .idx = id + 10u, .flags = 0, .name = fmt::format("test0.{}", id)});
         interface.createVLAN(id);
     }