blob: 6f441f7b40b49fc11e5135133b7ac58e75d9f7ef [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);
Johnathan Mantey817012a2020-01-30 15:07:39 -080062 return {bus, "/xyz/openbmc_test/network/test0",
63 EthernetInterface::DHCPConf::none, manager, true};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070064 }
65
Gunnar Mills57d9c502018-09-14 14:42:34 -050066 int countIPObjects()
67 {
68 return interface.getAddresses().size();
69 }
70
71 bool isIPObjectExist(const std::string& ipaddress)
72 {
73 auto address = interface.getAddresses().find(ipaddress);
74 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053075 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050076 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053077 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050078 return true;
79 }
Ratan Gupta255d5142017-08-10 09:02:08 +053080
Gunnar Mills57d9c502018-09-14 14:42:34 -050081 bool deleteIPObject(const std::string& ipaddress)
82 {
83 auto address = interface.getAddresses().find(ipaddress);
84 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053085 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050086 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053087 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050088 address->second->delete_();
89 return true;
90 }
Ratan Gupta255d5142017-08-10 09:02:08 +053091
Gunnar Mills57d9c502018-09-14 14:42:34 -050092 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
93 const std::string& gateway)
94 {
95 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053096
Gunnar Mills57d9c502018-09-14 14:42:34 -050097 return interface.generateObjectPath(addressType, ipaddress, subnetMask,
98 gateway);
99 }
100
101 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
102 uint8_t subnetMask, const std::string& gateway)
103 {
Patrick Williams6aef7692021-05-01 06:39:41 -0500104 interface.ip(addressType, ipaddress, subnetMask, gateway);
Gunnar Mills57d9c502018-09-14 14:42:34 -0500105 }
Ratan Gupta47722dc2017-05-26 18:32:23 +0530106};
107
108TEST_F(TestEthernetInterface, NoIPaddress)
109{
110 EXPECT_EQ(countIPObjects(), 0);
Patrick Williams6aef7692021-05-01 06:39:41 -0500111 EXPECT_EQ(mac_address::toString(mac), interface.macAddress());
Ratan Gupta47722dc2017-05-26 18:32:23 +0530112}
113
114TEST_F(TestEthernetInterface, AddIPAddress)
115{
116 IP::Protocol addressType = IP::Protocol::IPv4;
117 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
118 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530119}
120
121TEST_F(TestEthernetInterface, AddMultipleAddress)
122{
123 IP::Protocol addressType = IP::Protocol::IPv4;
124 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
125 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
126 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
127 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530128}
129
130TEST_F(TestEthernetInterface, DeleteIPAddress)
131{
132 IP::Protocol addressType = IP::Protocol::IPv4;
133 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
134 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
135 deleteIPObject("10.10.10.10");
136 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
137 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530138}
139
140TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
141{
142 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
143}
144
145TEST_F(TestEthernetInterface, CheckObjectPath)
146{
147 std::string ipaddress = "10.10.10.10";
148 uint8_t prefix = 16;
149 std::string gateway = "10.10.10.1";
150
151 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
152 std::stringstream hexId;
153
154 std::string hashString = ipaddress;
155 hashString += std::to_string(prefix);
156 hashString += gateway;
157
Gunnar Mills57d9c502018-09-14 14:42:34 -0500158 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530159 expectedObjectPath += hexId.str();
160
Gunnar Mills57d9c502018-09-14 14:42:34 -0500161 EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530162}
163
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530164TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530165{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500166 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
Ratan Guptab4005972019-09-19 06:19:16 +0530167 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530168 interface.staticNameServers(servers);
Ratan Gupta09f61572017-08-23 16:53:52 +0530169 fs::path filePath = confDir;
170 filePath /= "00-bmc-test0.network";
171 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530172 config::ReturnCode rc = config::ReturnCode::SUCCESS;
173 config::ValueList values;
174 std::tie(rc, values) = parser.getValues("Network", "DNS");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500175 EXPECT_EQ(servers, values);
Ratan Gupta09f61572017-08-23 16:53:52 +0530176}
177
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530178TEST_F(TestEthernetInterface, addDynamicNameServers)
179{
180 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
181 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
182 EXPECT_THROW(interface.nameservers(servers), NotAllowed);
183}
184
185TEST_F(TestEthernetInterface, getDynamicNameServers)
186{
187 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
188 EXPECT_CALL(interface, getNameServerFromResolvd())
189 .WillRepeatedly(testing::Return(servers));
190 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
191}
192
Ratan Guptac9645fe2017-08-23 16:53:52 +0530193TEST_F(TestEthernetInterface, addNTPServers)
194{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500195 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
Ratan Gupta35297172018-11-28 18:40:16 +0530196 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Patrick Williams6aef7692021-05-01 06:39:41 -0500197 interface.ntpServers(servers);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530198 fs::path filePath = confDir;
199 filePath /= "00-bmc-test0.network";
200 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530201 config::ReturnCode rc = config::ReturnCode::SUCCESS;
202 config::ValueList values;
203 std::tie(rc, values) = parser.getValues("Network", "NTP");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500204 EXPECT_EQ(servers, values);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530205}
206
Ravi Tejaa5a09442020-07-17 00:57:33 -0500207TEST_F(TestEthernetInterface, addGateway)
208{
209 std::string gateway = "10.3.3.3";
210 interface.defaultGateway(gateway);
211 EXPECT_EQ(interface.defaultGateway(), gateway);
212}
213
214TEST_F(TestEthernetInterface, addGateway6)
215{
216 std::string gateway6 = "ffff:ffff:ffff:fe80::1";
217 interface.defaultGateway6(gateway6);
218 EXPECT_EQ(interface.defaultGateway6(), gateway6);
219}
220
Gunnar Mills57d9c502018-09-14 14:42:34 -0500221} // namespace network
222} // namespace phosphor