blob: 654f45d9098841398b275f3ac332bf0d34554bc9 [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 IIIfd862be2022-10-09 18:40:55 -07005#include "system_queries.hpp"
William A. Kennington IIIcb64b992019-04-21 18:45:07 -07006#include "util.hpp"
Ratan Gupta47722dc2017-05-26 18:32:23 +05307
Ratan Gupta255d5142017-08-10 09:02:08 +05308#include <arpa/inet.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +05309#include <net/if.h>
10#include <netinet/in.h>
Ratan Gupta255d5142017-08-10 09:02:08 +053011#include <stdlib.h>
12
William A. Kennington III991a8e82022-10-11 15:02:47 -070013#include <charconv>
Ratan Gupta47722dc2017-05-26 18:32:23 +053014#include <exception>
Gunnar Mills57d9c502018-09-14 14:42:34 -050015#include <sdbusplus/bus.hpp>
William A. Kennington III84bfe672022-07-13 14:15:30 -070016#include <stdplus/gtest/tmp.hpp>
William A. Kennington III991a8e82022-10-11 15:02:47 -070017#include <string_view>
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060018#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050019
20#include <gtest/gtest.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +053021
22namespace phosphor
23{
24namespace network
25{
26
William A. Kennington III991a8e82022-10-11 15:02:47 -070027using std::literals::string_view_literals::operator""sv;
28
William A. Kennington III84bfe672022-07-13 14:15:30 -070029class TestEthernetInterface : public stdplus::gtest::TestWithTmp
Ratan Gupta47722dc2017-05-26 18:32:23 +053030{
Gunnar Mills57d9c502018-09-14 14:42:34 -050031 public:
Patrick Williamsc38b0712022-07-22 19:26:54 -050032 sdbusplus::bus_t bus;
William A. Kennington III84bfe672022-07-13 14:15:30 -070033 std::string confDir;
Ratan Gupta35297172018-11-28 18:40:16 +053034 MockManager manager;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053035 MockEthernetInterface interface;
Gunnar Mills57d9c502018-09-14 14:42:34 -050036 TestEthernetInterface() :
William A. Kennington III84bfe672022-07-13 14:15:30 -070037 bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
38 manager(bus, "/xyz/openbmc_test/network", confDir),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070039 interface(makeInterface(bus, manager))
Ratan Gupta47722dc2017-05-26 18:32:23 +053040
Gunnar Mills57d9c502018-09-14 14:42:34 -050041 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050042 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053043
Patrick Williamsc38b0712022-07-22 19:26:54 -050044 static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus,
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053045 MockManager& manager)
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070046 {
William A. Kennington III77848562022-10-26 17:32:57 -070047 system::mock_clear();
48 system::InterfaceInfo info{.idx = 1, .flags = 0, .name = "test0"};
49 system::mock_addIF(info);
50 return {bus, manager, info, "/xyz/openbmc_test/network"sv,
51 config::Parser()};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070052 }
53
Gunnar Mills57d9c502018-09-14 14:42:34 -050054 int countIPObjects()
55 {
56 return interface.getAddresses().size();
57 }
58
59 bool isIPObjectExist(const std::string& ipaddress)
60 {
61 auto address = interface.getAddresses().find(ipaddress);
62 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053063 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050064 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053065 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050066 return true;
67 }
Ratan Gupta255d5142017-08-10 09:02:08 +053068
Gunnar Mills57d9c502018-09-14 14:42:34 -050069 bool deleteIPObject(const std::string& ipaddress)
70 {
71 auto address = interface.getAddresses().find(ipaddress);
72 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053073 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050074 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053075 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050076 address->second->delete_();
77 return true;
78 }
Ratan Gupta255d5142017-08-10 09:02:08 +053079
Gunnar Mills57d9c502018-09-14 14:42:34 -050080 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
Lei YU34027572021-08-11 15:23:52 +080081 IP::AddressOrigin origin)
Gunnar Mills57d9c502018-09-14 14:42:34 -050082 {
83 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053084
Gunnar Mills57d9c502018-09-14 14:42:34 -050085 return interface.generateObjectPath(addressType, ipaddress, subnetMask,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070086 origin);
Gunnar Mills57d9c502018-09-14 14:42:34 -050087 }
88
89 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070090 uint8_t subnetMask)
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 {
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070092 interface.ip(addressType, ipaddress, subnetMask, "");
Gunnar Mills57d9c502018-09-14 14:42:34 -050093 }
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060094
95 void setNtpServers()
96 {
97 ServerList ntpServers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
98 interface.EthernetInterfaceIntf::ntpServers(ntpServers);
99 }
100
101 ServerList getNtpServers()
102 {
103 return interface.EthernetInterfaceIntf::ntpServers();
104 }
Ratan Gupta47722dc2017-05-26 18:32:23 +0530105};
106
William A. Kennington III9ecb90e2022-10-14 03:12:43 -0700107TEST_F(TestEthernetInterface, Fields)
108{
109 EXPECT_EQ(0, interface.mtu());
110 EXPECT_EQ("", interface.macAddress());
111 EXPECT_FALSE(interface.linkUp());
112
William A. Kennington III9ecb90e2022-10-14 03:12:43 -0700113 constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
114 constexpr unsigned mtu = 150;
115
William A. Kennington III77848562022-10-26 17:32:57 -0700116 system::InterfaceInfo info{.idx = 2,
117 .flags = IFF_RUNNING,
118 .name = "test1",
119 .mac = mac,
120 .mtu = mtu};
121 system::mock_addIF(info);
122 MockEthernetInterface intf(bus, manager, info,
William A. Kennington IIId298f932022-10-17 14:31:38 -0700123 "/xyz/openbmc_test/network"sv, config::Parser());
William A. Kennington III9ecb90e2022-10-14 03:12:43 -0700124
125 EXPECT_EQ(mtu, intf.mtu());
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700126 EXPECT_EQ(std::to_string(mac), intf.macAddress());
William A. Kennington III9ecb90e2022-10-14 03:12:43 -0700127 EXPECT_TRUE(intf.linkUp());
128}
129
Ratan Gupta47722dc2017-05-26 18:32:23 +0530130TEST_F(TestEthernetInterface, NoIPaddress)
131{
132 EXPECT_EQ(countIPObjects(), 0);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530133}
134
135TEST_F(TestEthernetInterface, AddIPAddress)
136{
137 IP::Protocol addressType = IP::Protocol::IPv4;
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700138 createIPObject(addressType, "10.10.10.10", 16);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530139 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530140}
141
142TEST_F(TestEthernetInterface, AddMultipleAddress)
143{
144 IP::Protocol addressType = IP::Protocol::IPv4;
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700145 createIPObject(addressType, "10.10.10.10", 16);
146 createIPObject(addressType, "20.20.20.20", 16);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530147 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
148 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530149}
150
151TEST_F(TestEthernetInterface, DeleteIPAddress)
152{
153 IP::Protocol addressType = IP::Protocol::IPv4;
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700154 createIPObject(addressType, "10.10.10.10", 16);
155 createIPObject(addressType, "20.20.20.20", 16);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530156 deleteIPObject("10.10.10.10");
157 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
158 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530159}
160
161TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
162{
163 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
164}
165
166TEST_F(TestEthernetInterface, CheckObjectPath)
167{
168 std::string ipaddress = "10.10.10.10";
169 uint8_t prefix = 16;
Lei YU34027572021-08-11 15:23:52 +0800170 IP::AddressOrigin origin = IP::AddressOrigin::Static;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530171
William A. Kennington III991a8e82022-10-11 15:02:47 -0700172 auto path = getObjectPath(ipaddress, prefix, origin);
173 auto pathsv = std::string_view(path);
174 constexpr auto expectedPrefix = "/xyz/openbmc_test/network/test0/ipv4/"sv;
175 EXPECT_TRUE(pathsv.starts_with(expectedPrefix));
176 pathsv.remove_prefix(expectedPrefix.size());
177 uint32_t val;
178 auto [ptr, res] = std::from_chars(pathsv.begin(), pathsv.end(), val, 16);
179 EXPECT_EQ(res, std::errc());
180 EXPECT_EQ(ptr, pathsv.end());
Ratan Gupta47722dc2017-05-26 18:32:23 +0530181
William A. Kennington III991a8e82022-10-11 15:02:47 -0700182 EXPECT_EQ(path, getObjectPath(ipaddress, prefix, origin));
Lei YU34027572021-08-11 15:23:52 +0800183 origin = IP::AddressOrigin::DHCP;
William A. Kennington III991a8e82022-10-11 15:02:47 -0700184 EXPECT_NE(path, getObjectPath(ipaddress, prefix, origin));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530185}
186
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530187TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530188{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500189 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700190 EXPECT_CALL(manager, reloadConfigs());
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530191 interface.staticNameServers(servers);
Ratan Gupta09f61572017-08-23 16:53:52 +0530192 fs::path filePath = confDir;
193 filePath /= "00-bmc-test0.network";
194 config::Parser parser(filePath.string());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700195 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "DNS"));
Ratan Gupta09f61572017-08-23 16:53:52 +0530196}
197
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530198TEST_F(TestEthernetInterface, getDynamicNameServers)
199{
200 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
201 EXPECT_CALL(interface, getNameServerFromResolvd())
202 .WillRepeatedly(testing::Return(servers));
203 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
204}
205
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600206TEST_F(TestEthernetInterface, addStaticNTPServers)
Ratan Guptac9645fe2017-08-23 16:53:52 +0530207{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500208 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700209 EXPECT_CALL(manager, reloadConfigs());
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600210 interface.staticNTPServers(servers);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530211 fs::path filePath = confDir;
212 filePath /= "00-bmc-test0.network";
213 config::Parser parser(filePath.string());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700214 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP"));
Ratan Guptac9645fe2017-08-23 16:53:52 +0530215}
216
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600217TEST_F(TestEthernetInterface, addNTPServers)
218{
219 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
220 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
221 EXPECT_THROW(interface.ntpServers(servers), NotAllowed);
222}
223
224TEST_F(TestEthernetInterface, getNTPServers)
225{
226 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
227 setNtpServers();
228 EXPECT_EQ(getNtpServers(), servers);
229}
230
Ravi Tejaa5a09442020-07-17 00:57:33 -0500231TEST_F(TestEthernetInterface, addGateway)
232{
233 std::string gateway = "10.3.3.3";
234 interface.defaultGateway(gateway);
235 EXPECT_EQ(interface.defaultGateway(), gateway);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800236 interface.defaultGateway("");
237 EXPECT_EQ(interface.defaultGateway(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500238}
239
240TEST_F(TestEthernetInterface, addGateway6)
241{
242 std::string gateway6 = "ffff:ffff:ffff:fe80::1";
243 interface.defaultGateway6(gateway6);
244 EXPECT_EQ(interface.defaultGateway6(), gateway6);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800245 interface.defaultGateway6("");
246 EXPECT_EQ(interface.defaultGateway6(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500247}
248
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700249TEST_F(TestEthernetInterface, DHCPEnabled)
250{
251 EXPECT_CALL(manager, reloadConfigs()).WillRepeatedly(testing::Return());
252
253 using DHCPConf = EthernetInterfaceIntf::DHCPConf;
254 auto test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
255 EXPECT_EQ(conf, interface.dhcpEnabled());
256 EXPECT_EQ(dhcp4, interface.dhcp4());
257 EXPECT_EQ(dhcp6, interface.dhcp6());
258 EXPECT_EQ(ra, interface.ipv6AcceptRA());
259 };
260 test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
261
262 auto set_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
263 EXPECT_EQ(conf, interface.dhcpEnabled(conf));
264 test(conf, dhcp4, dhcp6, ra);
265 };
266 set_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
267 set_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
268 set_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
269 /*ra=*/true);
270 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
271 set_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
272 /*ra=*/true);
273 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
274
275 auto ind_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
276 EXPECT_EQ(dhcp4, interface.dhcp4(dhcp4));
277 EXPECT_EQ(dhcp6, interface.dhcp6(dhcp6));
278 EXPECT_EQ(ra, interface.ipv6AcceptRA(ra));
279 test(conf, dhcp4, dhcp6, ra);
280 };
281 ind_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
282 ind_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
283 ind_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
284 /*ra=*/true);
285 ind_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/false);
286 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
287 ind_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
288 /*ra=*/true);
289 ind_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/false);
290 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
291}
292
Gunnar Mills57d9c502018-09-14 14:42:34 -0500293} // namespace network
294} // namespace phosphor