blob: d0beef700351b7adff46a7d78a00c946604376ed [file] [log] [blame]
Ratan Guptac9645fe2017-08-23 16:53:52 +05301#include "config_parser.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05302#include "ipaddress.hpp"
Ratan Gupta35297172018-11-28 18:40:16 +05303#include "mock_network_manager.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05004#include "mock_syscall.hpp"
William A. Kennington IIIcb64b992019-04-21 18:45:07 -07005#include "util.hpp"
Ratan Gupta47722dc2017-05-26 18:32:23 +05306
Ratan Gupta255d5142017-08-10 09:02:08 +05307#include <arpa/inet.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +05308#include <net/if.h>
9#include <netinet/in.h>
Ratan Gupta255d5142017-08-10 09:02:08 +053010#include <stdlib.h>
11
Ratan Gupta47722dc2017-05-26 18:32:23 +053012#include <exception>
Ratan Guptac9645fe2017-08-23 16:53:52 +053013#include <fstream>
Gunnar Mills57d9c502018-09-14 14:42:34 -050014#include <sdbusplus/bus.hpp>
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053015#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050016
17#include <gtest/gtest.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +053018
19namespace phosphor
20{
21namespace network
22{
23
24class TestEthernetInterface : public testing::Test
25{
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 public:
27 sdbusplus::bus::bus bus;
Ratan Gupta35297172018-11-28 18:40:16 +053028 MockManager manager;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053029 MockEthernetInterface interface;
Gunnar Mills57d9c502018-09-14 14:42:34 -050030 std::string confDir;
31 TestEthernetInterface() :
32 bus(sdbusplus::bus::new_default()),
33 manager(bus, "/xyz/openbmc_test/network", "/tmp/"),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070034 interface(makeInterface(bus, manager))
Ratan Gupta47722dc2017-05-26 18:32:23 +053035
Gunnar Mills57d9c502018-09-14 14:42:34 -050036 {
37 setConfDir();
38 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053039
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 void setConfDir()
41 {
42 char tmp[] = "/tmp/EthernetInterface.XXXXXX";
43 confDir = mkdtemp(tmp);
44 manager.setConfDir(confDir);
45 }
46
47 ~TestEthernetInterface()
48 {
49 if (confDir != "")
Ratan Gupta47722dc2017-05-26 18:32:23 +053050 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 fs::remove_all(confDir);
Ratan Gupta47722dc2017-05-26 18:32:23 +053052 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050053 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053054
William A. Kennington IIIcb64b992019-04-21 18:45:07 -070055 static constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
56
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053057 static MockEthernetInterface makeInterface(sdbusplus::bus::bus& bus,
58 MockManager& manager)
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070059 {
William A. Kennington III862275a2019-04-22 20:37:08 -070060 mock_clear();
William A. Kennington IIIcb64b992019-04-21 18:45:07 -070061 mock_addIF("test0", 1, mac);
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053062 return {bus, "/xyz/openbmc_test/network/test0", false, manager, true};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070063 }
64
Gunnar Mills57d9c502018-09-14 14:42:34 -050065 int countIPObjects()
66 {
67 return interface.getAddresses().size();
68 }
69
70 bool isIPObjectExist(const std::string& ipaddress)
71 {
72 auto address = interface.getAddresses().find(ipaddress);
73 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053074 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050075 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053076 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 return true;
78 }
Ratan Gupta255d5142017-08-10 09:02:08 +053079
Gunnar Mills57d9c502018-09-14 14:42:34 -050080 bool deleteIPObject(const std::string& ipaddress)
81 {
82 auto address = interface.getAddresses().find(ipaddress);
83 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053084 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050085 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053086 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050087 address->second->delete_();
88 return true;
89 }
Ratan Gupta255d5142017-08-10 09:02:08 +053090
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
92 const std::string& gateway)
93 {
94 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053095
Gunnar Mills57d9c502018-09-14 14:42:34 -050096 return interface.generateObjectPath(addressType, ipaddress, subnetMask,
97 gateway);
98 }
99
100 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
101 uint8_t subnetMask, const std::string& gateway)
102 {
103 interface.iP(addressType, ipaddress, subnetMask, gateway);
104 }
Ratan Gupta47722dc2017-05-26 18:32:23 +0530105};
106
107TEST_F(TestEthernetInterface, NoIPaddress)
108{
109 EXPECT_EQ(countIPObjects(), 0);
William A. Kennington IIIcb64b992019-04-21 18:45:07 -0700110 EXPECT_EQ(mac_address::toString(mac), interface.mACAddress());
Ratan Gupta47722dc2017-05-26 18:32:23 +0530111}
112
113TEST_F(TestEthernetInterface, AddIPAddress)
114{
115 IP::Protocol addressType = IP::Protocol::IPv4;
116 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
117 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530118}
119
120TEST_F(TestEthernetInterface, AddMultipleAddress)
121{
122 IP::Protocol addressType = IP::Protocol::IPv4;
123 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
124 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
125 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
126 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530127}
128
129TEST_F(TestEthernetInterface, DeleteIPAddress)
130{
131 IP::Protocol addressType = IP::Protocol::IPv4;
132 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
133 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
134 deleteIPObject("10.10.10.10");
135 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
136 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530137}
138
139TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
140{
141 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
142}
143
144TEST_F(TestEthernetInterface, CheckObjectPath)
145{
146 std::string ipaddress = "10.10.10.10";
147 uint8_t prefix = 16;
148 std::string gateway = "10.10.10.1";
149
150 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
151 std::stringstream hexId;
152
153 std::string hashString = ipaddress;
154 hashString += std::to_string(prefix);
155 hashString += gateway;
156
Gunnar Mills57d9c502018-09-14 14:42:34 -0500157 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530158 expectedObjectPath += hexId.str();
159
Gunnar Mills57d9c502018-09-14 14:42:34 -0500160 EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530161}
162
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530163TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530164{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500165 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
Ratan Guptab4005972019-09-19 06:19:16 +0530166 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530167 interface.staticNameServers(servers);
Ratan Gupta09f61572017-08-23 16:53:52 +0530168 fs::path filePath = confDir;
169 filePath /= "00-bmc-test0.network";
170 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530171 config::ReturnCode rc = config::ReturnCode::SUCCESS;
172 config::ValueList values;
173 std::tie(rc, values) = parser.getValues("Network", "DNS");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500174 EXPECT_EQ(servers, values);
Ratan Gupta09f61572017-08-23 16:53:52 +0530175}
176
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530177TEST_F(TestEthernetInterface, addDynamicNameServers)
178{
179 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
180 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
181 EXPECT_THROW(interface.nameservers(servers), NotAllowed);
182}
183
184TEST_F(TestEthernetInterface, getDynamicNameServers)
185{
186 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
187 EXPECT_CALL(interface, getNameServerFromResolvd())
188 .WillRepeatedly(testing::Return(servers));
189 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
190}
191
Ratan Guptac9645fe2017-08-23 16:53:52 +0530192TEST_F(TestEthernetInterface, addNTPServers)
193{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500194 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
Ratan Gupta35297172018-11-28 18:40:16 +0530195 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530196 interface.nTPServers(servers);
197 fs::path filePath = confDir;
198 filePath /= "00-bmc-test0.network";
199 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530200 config::ReturnCode rc = config::ReturnCode::SUCCESS;
201 config::ValueList values;
202 std::tie(rc, values) = parser.getValues("Network", "NTP");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500203 EXPECT_EQ(servers, values);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530204}
205
Gunnar Mills57d9c502018-09-14 14:42:34 -0500206} // namespace network
207} // namespace phosphor