blob: bd03afff97bfe66f699c5d81ecdd260238ba85d1 [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
William A. Kennington III59e5b912022-11-02 02:49:46 -070020#include <gmock/gmock.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -050021#include <gtest/gtest.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +053022
23namespace phosphor
24{
25namespace network
26{
27
William A. Kennington III991a8e82022-10-11 15:02:47 -070028using std::literals::string_view_literals::operator""sv;
William A. Kennington III59e5b912022-11-02 02:49:46 -070029using testing::Key;
30using testing::UnorderedElementsAre;
William A. Kennington III991a8e82022-10-11 15:02:47 -070031
William A. Kennington III84bfe672022-07-13 14:15:30 -070032class TestEthernetInterface : public stdplus::gtest::TestWithTmp
Ratan Gupta47722dc2017-05-26 18:32:23 +053033{
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 public:
Patrick Williamsc38b0712022-07-22 19:26:54 -050035 sdbusplus::bus_t bus;
William A. Kennington III84bfe672022-07-13 14:15:30 -070036 std::string confDir;
Ratan Gupta35297172018-11-28 18:40:16 +053037 MockManager manager;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053038 MockEthernetInterface interface;
Gunnar Mills57d9c502018-09-14 14:42:34 -050039 TestEthernetInterface() :
William A. Kennington III84bfe672022-07-13 14:15:30 -070040 bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
41 manager(bus, "/xyz/openbmc_test/network", confDir),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070042 interface(makeInterface(bus, manager))
Ratan Gupta47722dc2017-05-26 18:32:23 +053043
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053046
Patrick Williamsc38b0712022-07-22 19:26:54 -050047 static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus,
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053048 MockManager& manager)
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070049 {
William A. Kennington III77848562022-10-26 17:32:57 -070050 system::mock_clear();
51 system::InterfaceInfo info{.idx = 1, .flags = 0, .name = "test0"};
52 system::mock_addIF(info);
53 return {bus, manager, info, "/xyz/openbmc_test/network"sv,
54 config::Parser()};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070055 }
56
William A. Kennington III59e5b912022-11-02 02:49:46 -070057 auto createIPObject(IP::Protocol addressType, const std::string& ipaddress,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070058 uint8_t subnetMask)
Gunnar Mills57d9c502018-09-14 14:42:34 -050059 {
William A. Kennington III59e5b912022-11-02 02:49:46 -070060 return interface.ip(addressType, ipaddress, subnetMask, "");
Gunnar Mills57d9c502018-09-14 14:42:34 -050061 }
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060062
63 void setNtpServers()
64 {
65 ServerList ntpServers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
66 interface.EthernetInterfaceIntf::ntpServers(ntpServers);
67 }
68
69 ServerList getNtpServers()
70 {
71 return interface.EthernetInterfaceIntf::ntpServers();
72 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053073};
74
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070075TEST_F(TestEthernetInterface, Fields)
76{
77 EXPECT_EQ(0, interface.mtu());
78 EXPECT_EQ("", interface.macAddress());
79 EXPECT_FALSE(interface.linkUp());
80
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070081 constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
82 constexpr unsigned mtu = 150;
83
William A. Kennington III77848562022-10-26 17:32:57 -070084 system::InterfaceInfo info{.idx = 2,
85 .flags = IFF_RUNNING,
86 .name = "test1",
87 .mac = mac,
88 .mtu = mtu};
89 system::mock_addIF(info);
90 MockEthernetInterface intf(bus, manager, info,
William A. Kennington IIId298f932022-10-17 14:31:38 -070091 "/xyz/openbmc_test/network"sv, config::Parser());
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070092
93 EXPECT_EQ(mtu, intf.mtu());
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -070094 EXPECT_EQ(std::to_string(mac), intf.macAddress());
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070095 EXPECT_TRUE(intf.linkUp());
96}
97
Ratan Gupta47722dc2017-05-26 18:32:23 +053098TEST_F(TestEthernetInterface, NoIPaddress)
99{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700100 EXPECT_TRUE(interface.addrs.empty());
Ratan Gupta47722dc2017-05-26 18:32:23 +0530101}
102
103TEST_F(TestEthernetInterface, AddIPAddress)
104{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700105 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
106 EXPECT_THAT(interface.addrs, UnorderedElementsAre(Key(
107 IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530108}
109
110TEST_F(TestEthernetInterface, AddMultipleAddress)
111{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700112 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
113 createIPObject(IP::Protocol::IPv4, "20.20.20.20", 16);
114 EXPECT_THAT(
115 interface.addrs,
116 UnorderedElementsAre(Key(IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16)),
117 Key(IfAddr(in_addr{htonl(0x14141414)}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530118}
119
120TEST_F(TestEthernetInterface, DeleteIPAddress)
121{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700122 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
123 createIPObject(IP::Protocol::IPv4, "20.20.20.20", 16);
124 interface.addrs.at(IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16))->delete_();
125 EXPECT_THAT(interface.addrs, UnorderedElementsAre(Key(
126 IfAddr(in_addr{htonl(0x14141414)}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530127}
128
129TEST_F(TestEthernetInterface, CheckObjectPath)
130{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700131 auto path = createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
132 EXPECT_EQ(path.parent_path(), "/xyz/openbmc_test/network/test0");
133 EXPECT_EQ(path.filename(), "10.10.10.10/16");
Ratan Gupta47722dc2017-05-26 18:32:23 +0530134}
135
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530136TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530137{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500138 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
William A. Kennington IIIffab00d2022-11-07 16:58:36 -0800139 EXPECT_CALL(manager, reloadConfigsNoRefresh());
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530140 interface.staticNameServers(servers);
Ratan Gupta09f61572017-08-23 16:53:52 +0530141 fs::path filePath = confDir;
142 filePath /= "00-bmc-test0.network";
143 config::Parser parser(filePath.string());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700144 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "DNS"));
Ratan Gupta09f61572017-08-23 16:53:52 +0530145}
146
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530147TEST_F(TestEthernetInterface, getDynamicNameServers)
148{
149 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
150 EXPECT_CALL(interface, getNameServerFromResolvd())
151 .WillRepeatedly(testing::Return(servers));
152 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
153}
154
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600155TEST_F(TestEthernetInterface, addStaticNTPServers)
Ratan Guptac9645fe2017-08-23 16:53:52 +0530156{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500157 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
William A. Kennington IIIffab00d2022-11-07 16:58:36 -0800158 EXPECT_CALL(manager, reloadConfigsNoRefresh());
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600159 interface.staticNTPServers(servers);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530160 fs::path filePath = confDir;
161 filePath /= "00-bmc-test0.network";
162 config::Parser parser(filePath.string());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700163 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP"));
Ratan Guptac9645fe2017-08-23 16:53:52 +0530164}
165
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600166TEST_F(TestEthernetInterface, addNTPServers)
167{
168 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
169 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
170 EXPECT_THROW(interface.ntpServers(servers), NotAllowed);
171}
172
173TEST_F(TestEthernetInterface, getNTPServers)
174{
175 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
176 setNtpServers();
177 EXPECT_EQ(getNtpServers(), servers);
178}
179
Ravi Tejaa5a09442020-07-17 00:57:33 -0500180TEST_F(TestEthernetInterface, addGateway)
181{
182 std::string gateway = "10.3.3.3";
183 interface.defaultGateway(gateway);
184 EXPECT_EQ(interface.defaultGateway(), gateway);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800185 interface.defaultGateway("");
186 EXPECT_EQ(interface.defaultGateway(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500187}
188
189TEST_F(TestEthernetInterface, addGateway6)
190{
191 std::string gateway6 = "ffff:ffff:ffff:fe80::1";
192 interface.defaultGateway6(gateway6);
193 EXPECT_EQ(interface.defaultGateway6(), gateway6);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800194 interface.defaultGateway6("");
195 EXPECT_EQ(interface.defaultGateway6(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500196}
197
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700198TEST_F(TestEthernetInterface, DHCPEnabled)
199{
200 EXPECT_CALL(manager, reloadConfigs()).WillRepeatedly(testing::Return());
201
202 using DHCPConf = EthernetInterfaceIntf::DHCPConf;
203 auto test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
204 EXPECT_EQ(conf, interface.dhcpEnabled());
205 EXPECT_EQ(dhcp4, interface.dhcp4());
206 EXPECT_EQ(dhcp6, interface.dhcp6());
207 EXPECT_EQ(ra, interface.ipv6AcceptRA());
208 };
209 test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
210
211 auto set_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
212 EXPECT_EQ(conf, interface.dhcpEnabled(conf));
213 test(conf, dhcp4, dhcp6, ra);
214 };
215 set_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
216 set_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
217 set_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
218 /*ra=*/true);
219 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
220 set_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
221 /*ra=*/true);
222 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
223
224 auto ind_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
225 EXPECT_EQ(dhcp4, interface.dhcp4(dhcp4));
226 EXPECT_EQ(dhcp6, interface.dhcp6(dhcp6));
227 EXPECT_EQ(ra, interface.ipv6AcceptRA(ra));
228 test(conf, dhcp4, dhcp6, ra);
229 };
230 ind_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
231 ind_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
232 ind_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
233 /*ra=*/true);
234 ind_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/false);
235 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
236 ind_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
237 /*ra=*/true);
238 ind_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/false);
239 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
240}
241
Gunnar Mills57d9c502018-09-14 14:42:34 -0500242} // namespace network
243} // namespace phosphor