blob: a84e0ebb14a219ef5db7f44faf4d5598bb3a797b [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,
Lei YU34027572021-08-11 15:23:52 +080093 const std::string& gateway,
94 IP::AddressOrigin origin)
Gunnar Mills57d9c502018-09-14 14:42:34 -050095 {
96 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053097
Gunnar Mills57d9c502018-09-14 14:42:34 -050098 return interface.generateObjectPath(addressType, ipaddress, subnetMask,
Lei YU34027572021-08-11 15:23:52 +080099 gateway, origin);
Gunnar Mills57d9c502018-09-14 14:42:34 -0500100 }
101
102 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
103 uint8_t subnetMask, const std::string& gateway)
104 {
Patrick Williams6aef7692021-05-01 06:39:41 -0500105 interface.ip(addressType, ipaddress, subnetMask, gateway);
Gunnar Mills57d9c502018-09-14 14:42:34 -0500106 }
Ratan Gupta47722dc2017-05-26 18:32:23 +0530107};
108
109TEST_F(TestEthernetInterface, NoIPaddress)
110{
111 EXPECT_EQ(countIPObjects(), 0);
Patrick Williams6aef7692021-05-01 06:39:41 -0500112 EXPECT_EQ(mac_address::toString(mac), interface.macAddress());
Ratan Gupta47722dc2017-05-26 18:32:23 +0530113}
114
115TEST_F(TestEthernetInterface, AddIPAddress)
116{
117 IP::Protocol addressType = IP::Protocol::IPv4;
118 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
119 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530120}
121
122TEST_F(TestEthernetInterface, AddMultipleAddress)
123{
124 IP::Protocol addressType = IP::Protocol::IPv4;
125 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
126 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
127 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
128 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530129}
130
131TEST_F(TestEthernetInterface, DeleteIPAddress)
132{
133 IP::Protocol addressType = IP::Protocol::IPv4;
134 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
135 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
136 deleteIPObject("10.10.10.10");
137 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
138 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530139}
140
141TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
142{
143 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
144}
145
146TEST_F(TestEthernetInterface, CheckObjectPath)
147{
148 std::string ipaddress = "10.10.10.10";
149 uint8_t prefix = 16;
150 std::string gateway = "10.10.10.1";
Lei YU34027572021-08-11 15:23:52 +0800151 IP::AddressOrigin origin = IP::AddressOrigin::Static;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530152
153 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
154 std::stringstream hexId;
155
156 std::string hashString = ipaddress;
157 hashString += std::to_string(prefix);
158 hashString += gateway;
Lei YU34027572021-08-11 15:23:52 +0800159 hashString += convertForMessage(origin);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530160
Gunnar Mills57d9c502018-09-14 14:42:34 -0500161 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530162 expectedObjectPath += hexId.str();
163
Lei YU34027572021-08-11 15:23:52 +0800164 EXPECT_EQ(expectedObjectPath,
165 getObjectPath(ipaddress, prefix, gateway, origin));
166
167 origin = IP::AddressOrigin::DHCP;
168 EXPECT_NE(expectedObjectPath,
169 getObjectPath(ipaddress, prefix, gateway, origin));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530170}
171
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530172TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530173{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500174 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
Ratan Guptab4005972019-09-19 06:19:16 +0530175 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530176 interface.staticNameServers(servers);
Ratan Gupta09f61572017-08-23 16:53:52 +0530177 fs::path filePath = confDir;
178 filePath /= "00-bmc-test0.network";
179 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530180 config::ReturnCode rc = config::ReturnCode::SUCCESS;
181 config::ValueList values;
182 std::tie(rc, values) = parser.getValues("Network", "DNS");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500183 EXPECT_EQ(servers, values);
Ratan Gupta09f61572017-08-23 16:53:52 +0530184}
185
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530186TEST_F(TestEthernetInterface, addDynamicNameServers)
187{
188 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
189 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
190 EXPECT_THROW(interface.nameservers(servers), NotAllowed);
191}
192
193TEST_F(TestEthernetInterface, getDynamicNameServers)
194{
195 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
196 EXPECT_CALL(interface, getNameServerFromResolvd())
197 .WillRepeatedly(testing::Return(servers));
198 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
199}
200
Ratan Guptac9645fe2017-08-23 16:53:52 +0530201TEST_F(TestEthernetInterface, addNTPServers)
202{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500203 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
Ratan Gupta35297172018-11-28 18:40:16 +0530204 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Patrick Williams6aef7692021-05-01 06:39:41 -0500205 interface.ntpServers(servers);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530206 fs::path filePath = confDir;
207 filePath /= "00-bmc-test0.network";
208 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530209 config::ReturnCode rc = config::ReturnCode::SUCCESS;
210 config::ValueList values;
211 std::tie(rc, values) = parser.getValues("Network", "NTP");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500212 EXPECT_EQ(servers, values);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530213}
214
Ravi Tejaa5a09442020-07-17 00:57:33 -0500215TEST_F(TestEthernetInterface, addGateway)
216{
217 std::string gateway = "10.3.3.3";
218 interface.defaultGateway(gateway);
219 EXPECT_EQ(interface.defaultGateway(), gateway);
220}
221
222TEST_F(TestEthernetInterface, addGateway6)
223{
224 std::string gateway6 = "ffff:ffff:ffff:fe80::1";
225 interface.defaultGateway6(gateway6);
226 EXPECT_EQ(interface.defaultGateway6(), gateway6);
227}
228
Gunnar Mills57d9c502018-09-14 14:42:34 -0500229} // namespace network
230} // namespace phosphor