blob: 24fe5fb326dad0af0d563d0b3c3f7242c4da5dde [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>
William A. Kennington III84bfe672022-07-13 14:15:30 -070015#include <stdplus/gtest/tmp.hpp>
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053016#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050017
18#include <gtest/gtest.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +053019
20namespace phosphor
21{
22namespace network
23{
24
William A. Kennington III84bfe672022-07-13 14:15:30 -070025class TestEthernetInterface : public stdplus::gtest::TestWithTmp
Ratan Gupta47722dc2017-05-26 18:32:23 +053026{
Gunnar Mills57d9c502018-09-14 14:42:34 -050027 public:
Patrick Williamsc38b0712022-07-22 19:26:54 -050028 sdbusplus::bus_t bus;
William A. Kennington III84bfe672022-07-13 14:15:30 -070029 std::string confDir;
Ratan Gupta35297172018-11-28 18:40:16 +053030 MockManager manager;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053031 MockEthernetInterface interface;
Gunnar Mills57d9c502018-09-14 14:42:34 -050032 TestEthernetInterface() :
William A. Kennington III84bfe672022-07-13 14:15:30 -070033 bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
34 manager(bus, "/xyz/openbmc_test/network", confDir),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070035 interface(makeInterface(bus, manager))
Ratan Gupta47722dc2017-05-26 18:32:23 +053036
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050038 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053039
William A. Kennington IIIcb64b992019-04-21 18:45:07 -070040 static constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
41
Patrick Williamsc38b0712022-07-22 19:26:54 -050042 static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus,
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053043 MockManager& manager)
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070044 {
William A. Kennington III862275a2019-04-22 20:37:08 -070045 mock_clear();
William A. Kennington IIIcb64b992019-04-21 18:45:07 -070046 mock_addIF("test0", 1, mac);
Johnathan Mantey817012a2020-01-30 15:07:39 -080047 return {bus, "/xyz/openbmc_test/network/test0",
48 EthernetInterface::DHCPConf::none, manager, true};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070049 }
50
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 int countIPObjects()
52 {
53 return interface.getAddresses().size();
54 }
55
56 bool isIPObjectExist(const std::string& ipaddress)
57 {
58 auto address = interface.getAddresses().find(ipaddress);
59 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053060 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050061 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053062 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050063 return true;
64 }
Ratan Gupta255d5142017-08-10 09:02:08 +053065
Gunnar Mills57d9c502018-09-14 14:42:34 -050066 bool deleteIPObject(const std::string& ipaddress)
67 {
68 auto address = interface.getAddresses().find(ipaddress);
69 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053070 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050071 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053072 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 address->second->delete_();
74 return true;
75 }
Ratan Gupta255d5142017-08-10 09:02:08 +053076
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
Lei YU34027572021-08-11 15:23:52 +080078 const std::string& gateway,
79 IP::AddressOrigin origin)
Gunnar Mills57d9c502018-09-14 14:42:34 -050080 {
81 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053082
Gunnar Mills57d9c502018-09-14 14:42:34 -050083 return interface.generateObjectPath(addressType, ipaddress, subnetMask,
Lei YU34027572021-08-11 15:23:52 +080084 gateway, origin);
Gunnar Mills57d9c502018-09-14 14:42:34 -050085 }
86
87 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
88 uint8_t subnetMask, const std::string& gateway)
89 {
Patrick Williams6aef7692021-05-01 06:39:41 -050090 interface.ip(addressType, ipaddress, subnetMask, gateway);
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053092};
93
94TEST_F(TestEthernetInterface, NoIPaddress)
95{
96 EXPECT_EQ(countIPObjects(), 0);
Patrick Williams6aef7692021-05-01 06:39:41 -050097 EXPECT_EQ(mac_address::toString(mac), interface.macAddress());
Ratan Gupta47722dc2017-05-26 18:32:23 +053098}
99
100TEST_F(TestEthernetInterface, AddIPAddress)
101{
102 IP::Protocol addressType = IP::Protocol::IPv4;
103 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
104 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530105}
106
107TEST_F(TestEthernetInterface, AddMultipleAddress)
108{
109 IP::Protocol addressType = IP::Protocol::IPv4;
110 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
111 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
112 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
113 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530114}
115
116TEST_F(TestEthernetInterface, DeleteIPAddress)
117{
118 IP::Protocol addressType = IP::Protocol::IPv4;
119 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
120 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
121 deleteIPObject("10.10.10.10");
122 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
123 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530124}
125
126TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
127{
128 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
129}
130
131TEST_F(TestEthernetInterface, CheckObjectPath)
132{
133 std::string ipaddress = "10.10.10.10";
134 uint8_t prefix = 16;
135 std::string gateway = "10.10.10.1";
Lei YU34027572021-08-11 15:23:52 +0800136 IP::AddressOrigin origin = IP::AddressOrigin::Static;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530137
138 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
139 std::stringstream hexId;
140
141 std::string hashString = ipaddress;
142 hashString += std::to_string(prefix);
143 hashString += gateway;
Lei YU34027572021-08-11 15:23:52 +0800144 hashString += convertForMessage(origin);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530145
Gunnar Mills57d9c502018-09-14 14:42:34 -0500146 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530147 expectedObjectPath += hexId.str();
148
Lei YU34027572021-08-11 15:23:52 +0800149 EXPECT_EQ(expectedObjectPath,
150 getObjectPath(ipaddress, prefix, gateway, origin));
151
152 origin = IP::AddressOrigin::DHCP;
153 EXPECT_NE(expectedObjectPath,
154 getObjectPath(ipaddress, prefix, gateway, origin));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530155}
156
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530157TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530158{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500159 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700160 EXPECT_CALL(manager, reloadConfigs());
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530161 interface.staticNameServers(servers);
Ratan Gupta09f61572017-08-23 16:53:52 +0530162 fs::path filePath = confDir;
163 filePath /= "00-bmc-test0.network";
164 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530165 config::ReturnCode rc = config::ReturnCode::SUCCESS;
166 config::ValueList values;
167 std::tie(rc, values) = parser.getValues("Network", "DNS");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500168 EXPECT_EQ(servers, values);
Ratan Gupta09f61572017-08-23 16:53:52 +0530169}
170
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530171TEST_F(TestEthernetInterface, addDynamicNameServers)
172{
173 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
174 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
175 EXPECT_THROW(interface.nameservers(servers), NotAllowed);
176}
177
178TEST_F(TestEthernetInterface, getDynamicNameServers)
179{
180 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
181 EXPECT_CALL(interface, getNameServerFromResolvd())
182 .WillRepeatedly(testing::Return(servers));
183 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
184}
185
Ratan Guptac9645fe2017-08-23 16:53:52 +0530186TEST_F(TestEthernetInterface, addNTPServers)
187{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500188 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700189 EXPECT_CALL(manager, reloadConfigs());
Patrick Williams6aef7692021-05-01 06:39:41 -0500190 interface.ntpServers(servers);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530191 fs::path filePath = confDir;
192 filePath /= "00-bmc-test0.network";
193 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530194 config::ReturnCode rc = config::ReturnCode::SUCCESS;
195 config::ValueList values;
196 std::tie(rc, values) = parser.getValues("Network", "NTP");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500197 EXPECT_EQ(servers, values);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530198}
199
Ravi Tejaa5a09442020-07-17 00:57:33 -0500200TEST_F(TestEthernetInterface, addGateway)
201{
202 std::string gateway = "10.3.3.3";
203 interface.defaultGateway(gateway);
204 EXPECT_EQ(interface.defaultGateway(), gateway);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800205 interface.defaultGateway("");
206 EXPECT_EQ(interface.defaultGateway(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500207}
208
209TEST_F(TestEthernetInterface, addGateway6)
210{
211 std::string gateway6 = "ffff:ffff:ffff:fe80::1";
212 interface.defaultGateway6(gateway6);
213 EXPECT_EQ(interface.defaultGateway6(), gateway6);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800214 interface.defaultGateway6("");
215 EXPECT_EQ(interface.defaultGateway6(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500216}
217
Gunnar Mills57d9c502018-09-14 14:42:34 -0500218} // namespace network
219} // namespace phosphor