blob: 497eaa06246430e8ea0e5fc8f585d82102da0aed [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
William A. Kennington III991a8e82022-10-11 15:02:47 -070012#include <charconv>
Ratan Gupta47722dc2017-05-26 18:32:23 +053013#include <exception>
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>
William A. Kennington III991a8e82022-10-11 15:02:47 -070016#include <string_view>
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 III991a8e82022-10-11 15:02:47 -070025using std::literals::string_view_literals::operator""sv;
26
William A. Kennington III84bfe672022-07-13 14:15:30 -070027class TestEthernetInterface : public stdplus::gtest::TestWithTmp
Ratan Gupta47722dc2017-05-26 18:32:23 +053028{
Gunnar Mills57d9c502018-09-14 14:42:34 -050029 public:
Patrick Williamsc38b0712022-07-22 19:26:54 -050030 sdbusplus::bus_t bus;
William A. Kennington III84bfe672022-07-13 14:15:30 -070031 std::string confDir;
Ratan Gupta35297172018-11-28 18:40:16 +053032 MockManager manager;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053033 MockEthernetInterface interface;
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 TestEthernetInterface() :
William A. Kennington III84bfe672022-07-13 14:15:30 -070035 bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
36 manager(bus, "/xyz/openbmc_test/network", confDir),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070037 interface(makeInterface(bus, manager))
Ratan Gupta47722dc2017-05-26 18:32:23 +053038
Gunnar Mills57d9c502018-09-14 14:42:34 -050039 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053041
William A. Kennington IIIcb64b992019-04-21 18:45:07 -070042 static constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
43
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 III862275a2019-04-22 20:37:08 -070047 mock_clear();
William A. Kennington IIIcb64b992019-04-21 18:45:07 -070048 mock_addIF("test0", 1, mac);
William A. Kennington III0caf2212022-08-18 18:15:51 -070049 return {bus, "/xyz/openbmc_test/network/test0", config::Parser(),
50 manager, true};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070051 }
52
Gunnar Mills57d9c502018-09-14 14:42:34 -050053 int countIPObjects()
54 {
55 return interface.getAddresses().size();
56 }
57
58 bool isIPObjectExist(const std::string& ipaddress)
59 {
60 auto address = interface.getAddresses().find(ipaddress);
61 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053062 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050063 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053064 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050065 return true;
66 }
Ratan Gupta255d5142017-08-10 09:02:08 +053067
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 bool deleteIPObject(const std::string& ipaddress)
69 {
70 auto address = interface.getAddresses().find(ipaddress);
71 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053072 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053074 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050075 address->second->delete_();
76 return true;
77 }
Ratan Gupta255d5142017-08-10 09:02:08 +053078
Gunnar Mills57d9c502018-09-14 14:42:34 -050079 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
Lei YU34027572021-08-11 15:23:52 +080080 IP::AddressOrigin origin)
Gunnar Mills57d9c502018-09-14 14:42:34 -050081 {
82 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053083
Gunnar Mills57d9c502018-09-14 14:42:34 -050084 return interface.generateObjectPath(addressType, ipaddress, subnetMask,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070085 origin);
Gunnar Mills57d9c502018-09-14 14:42:34 -050086 }
87
88 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070089 uint8_t subnetMask)
Gunnar Mills57d9c502018-09-14 14:42:34 -050090 {
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070091 interface.ip(addressType, ipaddress, subnetMask, "");
Gunnar Mills57d9c502018-09-14 14:42:34 -050092 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053093};
94
95TEST_F(TestEthernetInterface, NoIPaddress)
96{
97 EXPECT_EQ(countIPObjects(), 0);
Patrick Williams6aef7692021-05-01 06:39:41 -050098 EXPECT_EQ(mac_address::toString(mac), interface.macAddress());
Ratan Gupta47722dc2017-05-26 18:32:23 +053099}
100
101TEST_F(TestEthernetInterface, AddIPAddress)
102{
103 IP::Protocol addressType = IP::Protocol::IPv4;
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700104 createIPObject(addressType, "10.10.10.10", 16);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530105 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530106}
107
108TEST_F(TestEthernetInterface, AddMultipleAddress)
109{
110 IP::Protocol addressType = IP::Protocol::IPv4;
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700111 createIPObject(addressType, "10.10.10.10", 16);
112 createIPObject(addressType, "20.20.20.20", 16);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530113 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
114 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530115}
116
117TEST_F(TestEthernetInterface, DeleteIPAddress)
118{
119 IP::Protocol addressType = IP::Protocol::IPv4;
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700120 createIPObject(addressType, "10.10.10.10", 16);
121 createIPObject(addressType, "20.20.20.20", 16);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530122 deleteIPObject("10.10.10.10");
123 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
124 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530125}
126
127TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
128{
129 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
130}
131
132TEST_F(TestEthernetInterface, CheckObjectPath)
133{
134 std::string ipaddress = "10.10.10.10";
135 uint8_t prefix = 16;
Lei YU34027572021-08-11 15:23:52 +0800136 IP::AddressOrigin origin = IP::AddressOrigin::Static;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530137
William A. Kennington III991a8e82022-10-11 15:02:47 -0700138 auto path = getObjectPath(ipaddress, prefix, origin);
139 auto pathsv = std::string_view(path);
140 constexpr auto expectedPrefix = "/xyz/openbmc_test/network/test0/ipv4/"sv;
141 EXPECT_TRUE(pathsv.starts_with(expectedPrefix));
142 pathsv.remove_prefix(expectedPrefix.size());
143 uint32_t val;
144 auto [ptr, res] = std::from_chars(pathsv.begin(), pathsv.end(), val, 16);
145 EXPECT_EQ(res, std::errc());
146 EXPECT_EQ(ptr, pathsv.end());
Ratan Gupta47722dc2017-05-26 18:32:23 +0530147
William A. Kennington III991a8e82022-10-11 15:02:47 -0700148 EXPECT_EQ(path, getObjectPath(ipaddress, prefix, origin));
Lei YU34027572021-08-11 15:23:52 +0800149 origin = IP::AddressOrigin::DHCP;
William A. Kennington III991a8e82022-10-11 15:02:47 -0700150 EXPECT_NE(path, getObjectPath(ipaddress, prefix, origin));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530151}
152
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530153TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530154{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500155 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700156 EXPECT_CALL(manager, reloadConfigs());
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530157 interface.staticNameServers(servers);
Ratan Gupta09f61572017-08-23 16:53:52 +0530158 fs::path filePath = confDir;
159 filePath /= "00-bmc-test0.network";
160 config::Parser parser(filePath.string());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700161 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "DNS"));
Ratan Gupta09f61572017-08-23 16:53:52 +0530162}
163
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530164TEST_F(TestEthernetInterface, getDynamicNameServers)
165{
166 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
167 EXPECT_CALL(interface, getNameServerFromResolvd())
168 .WillRepeatedly(testing::Return(servers));
169 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
170}
171
Ratan Guptac9645fe2017-08-23 16:53:52 +0530172TEST_F(TestEthernetInterface, addNTPServers)
173{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500174 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700175 EXPECT_CALL(manager, reloadConfigs());
Patrick Williams6aef7692021-05-01 06:39:41 -0500176 interface.ntpServers(servers);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530177 fs::path filePath = confDir;
178 filePath /= "00-bmc-test0.network";
179 config::Parser parser(filePath.string());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700180 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP"));
Ratan Guptac9645fe2017-08-23 16:53:52 +0530181}
182
Ravi Tejaa5a09442020-07-17 00:57:33 -0500183TEST_F(TestEthernetInterface, addGateway)
184{
185 std::string gateway = "10.3.3.3";
186 interface.defaultGateway(gateway);
187 EXPECT_EQ(interface.defaultGateway(), gateway);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800188 interface.defaultGateway("");
189 EXPECT_EQ(interface.defaultGateway(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500190}
191
192TEST_F(TestEthernetInterface, addGateway6)
193{
194 std::string gateway6 = "ffff:ffff:ffff:fe80::1";
195 interface.defaultGateway6(gateway6);
196 EXPECT_EQ(interface.defaultGateway6(), gateway6);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800197 interface.defaultGateway6("");
198 EXPECT_EQ(interface.defaultGateway6(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500199}
200
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700201TEST_F(TestEthernetInterface, DHCPEnabled)
202{
203 EXPECT_CALL(manager, reloadConfigs()).WillRepeatedly(testing::Return());
204
205 using DHCPConf = EthernetInterfaceIntf::DHCPConf;
206 auto test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
207 EXPECT_EQ(conf, interface.dhcpEnabled());
208 EXPECT_EQ(dhcp4, interface.dhcp4());
209 EXPECT_EQ(dhcp6, interface.dhcp6());
210 EXPECT_EQ(ra, interface.ipv6AcceptRA());
211 };
212 test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
213
214 auto set_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
215 EXPECT_EQ(conf, interface.dhcpEnabled(conf));
216 test(conf, dhcp4, dhcp6, ra);
217 };
218 set_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
219 set_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
220 set_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
221 /*ra=*/true);
222 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
223 set_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
224 /*ra=*/true);
225 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
226
227 auto ind_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
228 EXPECT_EQ(dhcp4, interface.dhcp4(dhcp4));
229 EXPECT_EQ(dhcp6, interface.dhcp6(dhcp6));
230 EXPECT_EQ(ra, interface.ipv6AcceptRA(ra));
231 test(conf, dhcp4, dhcp6, ra);
232 };
233 ind_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
234 ind_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
235 ind_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
236 /*ra=*/true);
237 ind_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/false);
238 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
239 ind_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
240 /*ra=*/true);
241 ind_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/false);
242 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
243}
244
Gunnar Mills57d9c502018-09-14 14:42:34 -0500245} // namespace network
246} // namespace phosphor