test: add test cases for ethernet interface
Change-Id: Ibf3a2b3513c784a56540488fa7de5a638a83a833
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index 5429bbc..0ec3f67 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -35,8 +35,8 @@
Manager& parent) :
Ifaces(bus, objPath.c_str(), true),
bus(bus),
- manager(parent)
-
+ manager(parent),
+ objPath(objPath)
{
auto intfName = objPath.substr(objPath.rfind("/") + 1);
interfaceName(intfName);
@@ -250,8 +250,7 @@
std::transform(type.begin(), type.end(), type.begin(), ::tolower);
std::experimental::filesystem::path objectPath;
- objectPath /= std::string(OBJ_NETWORK);
- objectPath /= interfaceName();
+ objectPath /= objPath;
objectPath /= type;
objectPath /= generateId(ipaddress, prefixLength, gateway);
return objectPath.string();
diff --git a/ethernet_interface.hpp b/ethernet_interface.hpp
index 7fb7cf1..856f874 100644
--- a/ethernet_interface.hpp
+++ b/ethernet_interface.hpp
@@ -25,6 +25,9 @@
using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
class Manager; // forward declaration of network manager.
+class Manager; // forward declaration of network manager.
+class TestEthernetInterface;
+
using LinkSpeed = uint16_t;
using DuplexMode = uint8_t;
using Autoneg = uint8_t;
@@ -134,6 +137,10 @@
/** @brief Persistent map of IPAddress dbus objects and their names */
AddressMap addrs;
+ /** @brief Dbus object path */
+ std::string objPath;
+
+ friend class TestEthernetInterface;
};
} // namespace network
diff --git a/test/Makefile.am b/test/Makefile.am
index b2fbced..87d0742 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -7,7 +7,8 @@
test_SOURCES = \
test_util.cpp \
mock_syscall.cpp \
- test_network_manager.cpp
+ test_network_manager.cpp \
+ test_ethernet_interface.cpp
test_CPPFLAGS = -Igtest $(GTEST_CPPFLAGS) $(AM_CPPFLAGS)
test_CXXFLAGS = $(PTHREAD_CFLAGS)
diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp
new file mode 100644
index 0000000..beaa0d4
--- /dev/null
+++ b/test/test_ethernet_interface.cpp
@@ -0,0 +1,151 @@
+#include "network_manager.hpp"
+#include "mock_syscall.hpp"
+
+#include <gtest/gtest.h>
+#include <sdbusplus/bus.hpp>
+
+#include <net/if.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <exception>
+
+namespace phosphor
+{
+namespace network
+{
+
+class TestEthernetInterface : public testing::Test
+{
+ public:
+
+ sdbusplus::bus::bus bus;
+ Manager manager;
+ EthernetInterface interface;
+ TestEthernetInterface()
+ : bus(sdbusplus::bus::new_default()),
+ manager(bus, "/xyz/openbmc_test/network"),
+ interface(bus, "/xyz/openbmc_test/network/test0", false, manager)
+
+ {
+
+ }
+
+ int countIPObjects()
+ {
+ return interface.getAddresses().size();
+ }
+
+ bool isIPObjectExist(const std::string& ipaddress)
+ {
+ auto address = interface.getAddresses().find(ipaddress);
+ if (address == interface.getAddresses().end())
+ {
+ return false;
+ }
+ return true;
+
+ }
+
+ bool deleteIPObject(const std::string& ipaddress)
+ {
+ auto address = interface.getAddresses().find(ipaddress);
+ if (address == interface.getAddresses().end())
+ {
+ return false;
+ }
+ address->second->delete_();
+ return true;
+ }
+
+ std::string getObjectPath(const std::string& ipaddress,
+ uint8_t subnetMask,
+ const std::string& gateway)
+ {
+ IP::Protocol addressType = IP::Protocol::IPv4;
+
+ return interface.generateObjectPath(addressType,
+ ipaddress,
+ subnetMask,
+ gateway);
+ }
+
+ void createIPObject(IP::Protocol addressType,
+ const std::string& ipaddress,
+ uint8_t subnetMask,
+ const std::string& gateway)
+ {
+ interface.iP(addressType,
+ ipaddress,
+ subnetMask,
+ gateway
+ );
+
+ }
+
+};
+
+TEST_F(TestEthernetInterface, NoIPaddress)
+{
+ EXPECT_EQ(countIPObjects(), 0);
+
+}
+
+TEST_F(TestEthernetInterface, AddIPAddress)
+{
+ IP::Protocol addressType = IP::Protocol::IPv4;
+ createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
+ EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
+
+}
+
+TEST_F(TestEthernetInterface, AddMultipleAddress)
+{
+ IP::Protocol addressType = IP::Protocol::IPv4;
+ createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
+ createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
+ EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
+ EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
+
+}
+
+TEST_F(TestEthernetInterface, DeleteIPAddress)
+{
+ IP::Protocol addressType = IP::Protocol::IPv4;
+ createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
+ createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
+ deleteIPObject("10.10.10.10");
+ EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
+ EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
+
+}
+
+TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
+{
+ EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
+}
+
+TEST_F(TestEthernetInterface, CheckObjectPath)
+{
+ std::string ipaddress = "10.10.10.10";
+ uint8_t prefix = 16;
+ std::string gateway = "10.10.10.1";
+
+ std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
+ std::stringstream hexId;
+
+ std::string hashString = ipaddress;
+ hashString += std::to_string(prefix);
+ hashString += gateway;
+
+
+ hexId << std::hex << ((std::hash<std::string> {}(
+ hashString)) & 0xFFFFFFFF);
+ expectedObjectPath += hexId.str();
+
+ EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress,
+ prefix,
+ gateway));
+}
+
+}// namespce network
+}// namespace phosphor