blob: 4fd97a053f07c4dfef420e88e500d6f1b05dc2ad [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>
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060017#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050018
19#include <gtest/gtest.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +053020
21namespace phosphor
22{
23namespace network
24{
25
William A. Kennington III991a8e82022-10-11 15:02:47 -070026using std::literals::string_view_literals::operator""sv;
27
William A. Kennington III84bfe672022-07-13 14:15:30 -070028class TestEthernetInterface : public stdplus::gtest::TestWithTmp
Ratan Gupta47722dc2017-05-26 18:32:23 +053029{
Gunnar Mills57d9c502018-09-14 14:42:34 -050030 public:
Patrick Williamsc38b0712022-07-22 19:26:54 -050031 sdbusplus::bus_t bus;
William A. Kennington III84bfe672022-07-13 14:15:30 -070032 std::string confDir;
Ratan Gupta35297172018-11-28 18:40:16 +053033 MockManager manager;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053034 MockEthernetInterface interface;
Gunnar Mills57d9c502018-09-14 14:42:34 -050035 TestEthernetInterface() :
William A. Kennington III84bfe672022-07-13 14:15:30 -070036 bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
37 manager(bus, "/xyz/openbmc_test/network", confDir),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070038 interface(makeInterface(bus, manager))
Ratan Gupta47722dc2017-05-26 18:32:23 +053039
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050041 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053042
Patrick Williamsc38b0712022-07-22 19:26:54 -050043 static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus,
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053044 MockManager& manager)
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070045 {
William A. Kennington III862275a2019-04-22 20:37:08 -070046 mock_clear();
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070047 mock_addIF("test0", /*idx=*/1);
William A. Kennington III0caf2212022-08-18 18:15:51 -070048 return {bus, "/xyz/openbmc_test/network/test0", config::Parser(),
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070049 manager};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070050 }
51
Gunnar Mills57d9c502018-09-14 14:42:34 -050052 int countIPObjects()
53 {
54 return interface.getAddresses().size();
55 }
56
57 bool isIPObjectExist(const std::string& ipaddress)
58 {
59 auto address = interface.getAddresses().find(ipaddress);
60 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053061 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050062 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053063 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050064 return true;
65 }
Ratan Gupta255d5142017-08-10 09:02:08 +053066
Gunnar Mills57d9c502018-09-14 14:42:34 -050067 bool deleteIPObject(const std::string& ipaddress)
68 {
69 auto address = interface.getAddresses().find(ipaddress);
70 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053071 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050072 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053073 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050074 address->second->delete_();
75 return true;
76 }
Ratan Gupta255d5142017-08-10 09:02:08 +053077
Gunnar Mills57d9c502018-09-14 14:42:34 -050078 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
Lei YU34027572021-08-11 15:23:52 +080079 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,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070084 origin);
Gunnar Mills57d9c502018-09-14 14:42:34 -050085 }
86
87 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070088 uint8_t subnetMask)
Gunnar Mills57d9c502018-09-14 14:42:34 -050089 {
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070090 interface.ip(addressType, ipaddress, subnetMask, "");
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 }
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060092
93 void setNtpServers()
94 {
95 ServerList ntpServers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
96 interface.EthernetInterfaceIntf::ntpServers(ntpServers);
97 }
98
99 ServerList getNtpServers()
100 {
101 return interface.EthernetInterfaceIntf::ntpServers();
102 }
Ratan Gupta47722dc2017-05-26 18:32:23 +0530103};
104
William A. Kennington III9ecb90e2022-10-14 03:12:43 -0700105TEST_F(TestEthernetInterface, Fields)
106{
107 EXPECT_EQ(0, interface.mtu());
108 EXPECT_EQ("", interface.macAddress());
109 EXPECT_FALSE(interface.linkUp());
110
111 constexpr unsigned idx = 2;
112 constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
113 constexpr unsigned mtu = 150;
114
115 mock_addIF("test1", idx, IFF_RUNNING, mac, mtu);
116 MockEthernetInterface intf(bus, "/xyz/openbmc_test/network/test1",
117 config::Parser(), manager);
118
119 EXPECT_EQ(mtu, intf.mtu());
120 EXPECT_EQ(mac_address::toString(mac), intf.macAddress());
121 EXPECT_TRUE(intf.linkUp());
122}
123
Ratan Gupta47722dc2017-05-26 18:32:23 +0530124TEST_F(TestEthernetInterface, NoIPaddress)
125{
126 EXPECT_EQ(countIPObjects(), 0);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530127}
128
129TEST_F(TestEthernetInterface, AddIPAddress)
130{
131 IP::Protocol addressType = IP::Protocol::IPv4;
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700132 createIPObject(addressType, "10.10.10.10", 16);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530133 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530134}
135
136TEST_F(TestEthernetInterface, AddMultipleAddress)
137{
138 IP::Protocol addressType = IP::Protocol::IPv4;
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700139 createIPObject(addressType, "10.10.10.10", 16);
140 createIPObject(addressType, "20.20.20.20", 16);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530141 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
142 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530143}
144
145TEST_F(TestEthernetInterface, DeleteIPAddress)
146{
147 IP::Protocol addressType = IP::Protocol::IPv4;
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700148 createIPObject(addressType, "10.10.10.10", 16);
149 createIPObject(addressType, "20.20.20.20", 16);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530150 deleteIPObject("10.10.10.10");
151 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
152 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530153}
154
155TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
156{
157 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
158}
159
160TEST_F(TestEthernetInterface, CheckObjectPath)
161{
162 std::string ipaddress = "10.10.10.10";
163 uint8_t prefix = 16;
Lei YU34027572021-08-11 15:23:52 +0800164 IP::AddressOrigin origin = IP::AddressOrigin::Static;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530165
William A. Kennington III991a8e82022-10-11 15:02:47 -0700166 auto path = getObjectPath(ipaddress, prefix, origin);
167 auto pathsv = std::string_view(path);
168 constexpr auto expectedPrefix = "/xyz/openbmc_test/network/test0/ipv4/"sv;
169 EXPECT_TRUE(pathsv.starts_with(expectedPrefix));
170 pathsv.remove_prefix(expectedPrefix.size());
171 uint32_t val;
172 auto [ptr, res] = std::from_chars(pathsv.begin(), pathsv.end(), val, 16);
173 EXPECT_EQ(res, std::errc());
174 EXPECT_EQ(ptr, pathsv.end());
Ratan Gupta47722dc2017-05-26 18:32:23 +0530175
William A. Kennington III991a8e82022-10-11 15:02:47 -0700176 EXPECT_EQ(path, getObjectPath(ipaddress, prefix, origin));
Lei YU34027572021-08-11 15:23:52 +0800177 origin = IP::AddressOrigin::DHCP;
William A. Kennington III991a8e82022-10-11 15:02:47 -0700178 EXPECT_NE(path, getObjectPath(ipaddress, prefix, origin));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530179}
180
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530181TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530182{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500183 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700184 EXPECT_CALL(manager, reloadConfigs());
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530185 interface.staticNameServers(servers);
Ratan Gupta09f61572017-08-23 16:53:52 +0530186 fs::path filePath = confDir;
187 filePath /= "00-bmc-test0.network";
188 config::Parser parser(filePath.string());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700189 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "DNS"));
Ratan Gupta09f61572017-08-23 16:53:52 +0530190}
191
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530192TEST_F(TestEthernetInterface, getDynamicNameServers)
193{
194 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
195 EXPECT_CALL(interface, getNameServerFromResolvd())
196 .WillRepeatedly(testing::Return(servers));
197 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
198}
199
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600200TEST_F(TestEthernetInterface, addStaticNTPServers)
Ratan Guptac9645fe2017-08-23 16:53:52 +0530201{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500202 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700203 EXPECT_CALL(manager, reloadConfigs());
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600204 interface.staticNTPServers(servers);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530205 fs::path filePath = confDir;
206 filePath /= "00-bmc-test0.network";
207 config::Parser parser(filePath.string());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700208 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP"));
Ratan Guptac9645fe2017-08-23 16:53:52 +0530209}
210
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600211TEST_F(TestEthernetInterface, addNTPServers)
212{
213 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
214 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
215 EXPECT_THROW(interface.ntpServers(servers), NotAllowed);
216}
217
218TEST_F(TestEthernetInterface, getNTPServers)
219{
220 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
221 setNtpServers();
222 EXPECT_EQ(getNtpServers(), servers);
223}
224
Ravi Tejaa5a09442020-07-17 00:57:33 -0500225TEST_F(TestEthernetInterface, addGateway)
226{
227 std::string gateway = "10.3.3.3";
228 interface.defaultGateway(gateway);
229 EXPECT_EQ(interface.defaultGateway(), gateway);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800230 interface.defaultGateway("");
231 EXPECT_EQ(interface.defaultGateway(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500232}
233
234TEST_F(TestEthernetInterface, addGateway6)
235{
236 std::string gateway6 = "ffff:ffff:ffff:fe80::1";
237 interface.defaultGateway6(gateway6);
238 EXPECT_EQ(interface.defaultGateway6(), gateway6);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800239 interface.defaultGateway6("");
240 EXPECT_EQ(interface.defaultGateway6(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500241}
242
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700243TEST_F(TestEthernetInterface, DHCPEnabled)
244{
245 EXPECT_CALL(manager, reloadConfigs()).WillRepeatedly(testing::Return());
246
247 using DHCPConf = EthernetInterfaceIntf::DHCPConf;
248 auto test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
249 EXPECT_EQ(conf, interface.dhcpEnabled());
250 EXPECT_EQ(dhcp4, interface.dhcp4());
251 EXPECT_EQ(dhcp6, interface.dhcp6());
252 EXPECT_EQ(ra, interface.ipv6AcceptRA());
253 };
254 test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
255
256 auto set_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
257 EXPECT_EQ(conf, interface.dhcpEnabled(conf));
258 test(conf, dhcp4, dhcp6, ra);
259 };
260 set_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
261 set_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
262 set_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
263 /*ra=*/true);
264 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
265 set_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
266 /*ra=*/true);
267 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
268
269 auto ind_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
270 EXPECT_EQ(dhcp4, interface.dhcp4(dhcp4));
271 EXPECT_EQ(dhcp6, interface.dhcp6(dhcp6));
272 EXPECT_EQ(ra, interface.ipv6AcceptRA(ra));
273 test(conf, dhcp4, dhcp6, ra);
274 };
275 ind_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
276 ind_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
277 ind_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
278 /*ra=*/true);
279 ind_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/false);
280 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
281 ind_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
282 /*ra=*/true);
283 ind_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/false);
284 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
285}
286
Gunnar Mills57d9c502018-09-14 14:42:34 -0500287} // namespace network
288} // namespace phosphor