blob: 0596980b971672570b8576c6818177e915348e70 [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"
William A. Kennington III5b179382022-11-15 15:23:26 -08003#include "mock_ethernet_interface.hpp"
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -08004#include "test_network_manager.hpp"
Ratan Gupta47722dc2017-05-26 18:32:23 +05305
6#include <net/if.h>
William A. Kennington III9c441fd2023-02-24 13:40:01 -08007#include <net/if_arp.h>
Ratan Gupta255d5142017-08-10 09:02:08 +05308
Gunnar Mills57d9c502018-09-14 14:42:34 -05009#include <sdbusplus/bus.hpp>
William A. Kennington III84bfe672022-07-13 14:15:30 -070010#include <stdplus/gtest/tmp.hpp>
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060011#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050012
Patrick Williams89d734b2023-05-10 07:50:25 -050013#include <string_view>
14
Gunnar Mills57d9c502018-09-14 14:42:34 -050015#include <gtest/gtest.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +053016
17namespace phosphor
18{
19namespace network
20{
21
William A. Kennington III991a8e82022-10-11 15:02:47 -070022using std::literals::string_view_literals::operator""sv;
William A. Kennington III59e5b912022-11-02 02:49:46 -070023using testing::Key;
24using testing::UnorderedElementsAre;
William A. Kennington III991a8e82022-10-11 15:02:47 -070025
William A. Kennington III84bfe672022-07-13 14:15:30 -070026class TestEthernetInterface : public stdplus::gtest::TestWithTmp
Ratan Gupta47722dc2017-05-26 18:32:23 +053027{
Gunnar Mills57d9c502018-09-14 14:42:34 -050028 public:
William A. Kennington III9ede1b72022-11-21 01:59:28 -080029 stdplus::Pinned<sdbusplus::bus_t> bus;
William A. Kennington III5b179382022-11-15 15:23:26 -080030 std::filesystem::path confDir;
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080031 TestManager manager;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053032 MockEthernetInterface interface;
Gunnar Mills57d9c502018-09-14 14:42:34 -050033 TestEthernetInterface() :
William A. Kennington III84bfe672022-07-13 14:15:30 -070034 bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
35 manager(bus, "/xyz/openbmc_test/network", confDir),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070036 interface(makeInterface(bus, manager))
Ratan Gupta47722dc2017-05-26 18:32:23 +053037
Patrick Williams89d734b2023-05-10 07:50:25 -050038 {}
Ratan Gupta47722dc2017-05-26 18:32:23 +053039
William A. Kennington III9ede1b72022-11-21 01:59:28 -080040 static MockEthernetInterface
41 makeInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus,
42 TestManager& manager)
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070043 {
William A. Kennington III9c441fd2023-02-24 13:40:01 -080044 AllIntfInfo info{InterfaceInfo{
45 .type = ARPHRD_ETHER, .idx = 1, .flags = 0, .name = "test0"}};
William A. Kennington III77848562022-10-26 17:32:57 -070046 return {bus, manager, info, "/xyz/openbmc_test/network"sv,
47 config::Parser()};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070048 }
49
William A. Kennington III59e5b912022-11-02 02:49:46 -070050 auto createIPObject(IP::Protocol addressType, const std::string& ipaddress,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070051 uint8_t subnetMask)
Gunnar Mills57d9c502018-09-14 14:42:34 -050052 {
William A. Kennington III59e5b912022-11-02 02:49:46 -070053 return interface.ip(addressType, ipaddress, subnetMask, "");
Gunnar Mills57d9c502018-09-14 14:42:34 -050054 }
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060055
56 void setNtpServers()
57 {
58 ServerList ntpServers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
59 interface.EthernetInterfaceIntf::ntpServers(ntpServers);
60 }
61
62 ServerList getNtpServers()
63 {
64 return interface.EthernetInterfaceIntf::ntpServers();
65 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053066};
67
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070068TEST_F(TestEthernetInterface, Fields)
69{
70 EXPECT_EQ(0, interface.mtu());
71 EXPECT_EQ("", interface.macAddress());
72 EXPECT_FALSE(interface.linkUp());
73
William A. Kennington IIIb7d6a1a2023-06-17 02:00:53 -070074 constexpr stdplus::EtherAddr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070075 constexpr unsigned mtu = 150;
76
William A. Kennington III9c441fd2023-02-24 13:40:01 -080077 AllIntfInfo info{InterfaceInfo{.type = ARPHRD_ETHER,
78 .idx = 2,
William A. Kennington III13d665c2022-11-15 20:34:40 -080079 .flags = IFF_RUNNING,
80 .name = "test1",
81 .mac = mac,
82 .mtu = mtu}};
William A. Kennington III77848562022-10-26 17:32:57 -070083 MockEthernetInterface intf(bus, manager, info,
William A. Kennington IIId298f932022-10-17 14:31:38 -070084 "/xyz/openbmc_test/network"sv, config::Parser());
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070085
86 EXPECT_EQ(mtu, intf.mtu());
William A. Kennington IIIb7d6a1a2023-06-17 02:00:53 -070087 EXPECT_EQ(stdplus::toStr(mac), intf.macAddress());
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070088 EXPECT_TRUE(intf.linkUp());
89}
90
Ratan Gupta47722dc2017-05-26 18:32:23 +053091TEST_F(TestEthernetInterface, NoIPaddress)
92{
William A. Kennington III59e5b912022-11-02 02:49:46 -070093 EXPECT_TRUE(interface.addrs.empty());
Ratan Gupta47722dc2017-05-26 18:32:23 +053094}
95
96TEST_F(TestEthernetInterface, AddIPAddress)
97{
William A. Kennington III59e5b912022-11-02 02:49:46 -070098 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
99 EXPECT_THAT(interface.addrs, UnorderedElementsAre(Key(
100 IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530101}
102
103TEST_F(TestEthernetInterface, AddMultipleAddress)
104{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700105 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
106 createIPObject(IP::Protocol::IPv4, "20.20.20.20", 16);
107 EXPECT_THAT(
108 interface.addrs,
109 UnorderedElementsAre(Key(IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16)),
110 Key(IfAddr(in_addr{htonl(0x14141414)}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530111}
112
113TEST_F(TestEthernetInterface, DeleteIPAddress)
114{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700115 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
116 createIPObject(IP::Protocol::IPv4, "20.20.20.20", 16);
117 interface.addrs.at(IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16))->delete_();
118 EXPECT_THAT(interface.addrs, UnorderedElementsAre(Key(
119 IfAddr(in_addr{htonl(0x14141414)}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530120}
121
122TEST_F(TestEthernetInterface, CheckObjectPath)
123{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700124 auto path = createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
125 EXPECT_EQ(path.parent_path(), "/xyz/openbmc_test/network/test0");
126 EXPECT_EQ(path.filename(), "10.10.10.10/16");
Ratan Gupta47722dc2017-05-26 18:32:23 +0530127}
128
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530129TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530130{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500131 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800132 EXPECT_CALL(manager.mockReload, schedule());
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530133 interface.staticNameServers(servers);
William A. Kennington III5b179382022-11-15 15:23:26 -0800134 config::Parser parser((confDir / "00-bmc-test0.network").native());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700135 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "DNS"));
Ratan Gupta09f61572017-08-23 16:53:52 +0530136}
137
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530138TEST_F(TestEthernetInterface, getDynamicNameServers)
139{
140 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
141 EXPECT_CALL(interface, getNameServerFromResolvd())
142 .WillRepeatedly(testing::Return(servers));
143 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
144}
145
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600146TEST_F(TestEthernetInterface, addStaticNTPServers)
Ratan Guptac9645fe2017-08-23 16:53:52 +0530147{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500148 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800149 EXPECT_CALL(manager.mockReload, schedule());
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600150 interface.staticNTPServers(servers);
William A. Kennington III5b179382022-11-15 15:23:26 -0800151 config::Parser parser((confDir / "00-bmc-test0.network").native());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700152 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP"));
Ratan Guptac9645fe2017-08-23 16:53:52 +0530153}
154
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600155TEST_F(TestEthernetInterface, addNTPServers)
156{
157 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
158 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
159 EXPECT_THROW(interface.ntpServers(servers), NotAllowed);
160}
161
162TEST_F(TestEthernetInterface, getNTPServers)
163{
164 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
165 setNtpServers();
166 EXPECT_EQ(getNtpServers(), servers);
167}
168
Ravi Tejaa5a09442020-07-17 00:57:33 -0500169TEST_F(TestEthernetInterface, addGateway)
170{
171 std::string gateway = "10.3.3.3";
172 interface.defaultGateway(gateway);
173 EXPECT_EQ(interface.defaultGateway(), gateway);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800174 interface.defaultGateway("");
175 EXPECT_EQ(interface.defaultGateway(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500176}
177
178TEST_F(TestEthernetInterface, addGateway6)
179{
180 std::string gateway6 = "ffff:ffff:ffff:fe80::1";
181 interface.defaultGateway6(gateway6);
182 EXPECT_EQ(interface.defaultGateway6(), gateway6);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800183 interface.defaultGateway6("");
184 EXPECT_EQ(interface.defaultGateway6(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500185}
186
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700187TEST_F(TestEthernetInterface, DHCPEnabled)
188{
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800189 EXPECT_CALL(manager.mockReload, schedule())
190 .WillRepeatedly(testing::Return());
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700191
192 using DHCPConf = EthernetInterfaceIntf::DHCPConf;
193 auto test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
194 EXPECT_EQ(conf, interface.dhcpEnabled());
195 EXPECT_EQ(dhcp4, interface.dhcp4());
196 EXPECT_EQ(dhcp6, interface.dhcp6());
197 EXPECT_EQ(ra, interface.ipv6AcceptRA());
198 };
199 test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
200
201 auto set_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
202 EXPECT_EQ(conf, interface.dhcpEnabled(conf));
203 test(conf, dhcp4, dhcp6, ra);
204 };
205 set_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
206 set_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
207 set_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
208 /*ra=*/true);
209 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
210 set_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
211 /*ra=*/true);
212 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
213
214 auto ind_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
215 EXPECT_EQ(dhcp4, interface.dhcp4(dhcp4));
216 EXPECT_EQ(dhcp6, interface.dhcp6(dhcp6));
217 EXPECT_EQ(ra, interface.ipv6AcceptRA(ra));
218 test(conf, dhcp4, dhcp6, ra);
219 };
220 ind_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
221 ind_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
222 ind_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
223 /*ra=*/true);
224 ind_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/false);
225 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
226 ind_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
227 /*ra=*/true);
228 ind_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/false);
229 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
230}
231
Gunnar Mills57d9c502018-09-14 14:42:34 -0500232} // namespace network
233} // namespace phosphor