blob: 74768c1cb95783226dd9fcb2120f57be2e4c286d [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 IIIbbf5e9e2023-02-01 14:58:38 -080022using sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
William A. Kennington III991a8e82022-10-11 15:02:47 -070023using std::literals::string_view_literals::operator""sv;
William A. Kennington III59e5b912022-11-02 02:49:46 -070024using testing::Key;
25using testing::UnorderedElementsAre;
William A. Kennington III991a8e82022-10-11 15:02:47 -070026
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:
William A. Kennington III9ede1b72022-11-21 01:59:28 -080030 stdplus::Pinned<sdbusplus::bus_t> bus;
William A. Kennington III5b179382022-11-15 15:23:26 -080031 std::filesystem::path confDir;
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080032 TestManager 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
Patrick Williams89d734b2023-05-10 07:50:25 -050039 {}
Ratan Gupta47722dc2017-05-26 18:32:23 +053040
William A. Kennington III9ede1b72022-11-21 01:59:28 -080041 static MockEthernetInterface
42 makeInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus,
43 TestManager& manager)
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070044 {
William A. Kennington III9c441fd2023-02-24 13:40:01 -080045 AllIntfInfo info{InterfaceInfo{
46 .type = ARPHRD_ETHER, .idx = 1, .flags = 0, .name = "test0"}};
William A. Kennington III77848562022-10-26 17:32:57 -070047 return {bus, manager, info, "/xyz/openbmc_test/network"sv,
48 config::Parser()};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070049 }
50
William A. Kennington III59e5b912022-11-02 02:49:46 -070051 auto createIPObject(IP::Protocol addressType, const std::string& ipaddress,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070052 uint8_t subnetMask)
Gunnar Mills57d9c502018-09-14 14:42:34 -050053 {
William A. Kennington III59e5b912022-11-02 02:49:46 -070054 return interface.ip(addressType, ipaddress, subnetMask, "");
Gunnar Mills57d9c502018-09-14 14:42:34 -050055 }
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060056
57 void setNtpServers()
58 {
59 ServerList ntpServers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
60 interface.EthernetInterfaceIntf::ntpServers(ntpServers);
61 }
62
63 ServerList getNtpServers()
64 {
65 return interface.EthernetInterfaceIntf::ntpServers();
66 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053067};
68
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070069TEST_F(TestEthernetInterface, Fields)
70{
71 EXPECT_EQ(0, interface.mtu());
72 EXPECT_EQ("", interface.macAddress());
73 EXPECT_FALSE(interface.linkUp());
74
William A. Kennington IIIb7d6a1a2023-06-17 02:00:53 -070075 constexpr stdplus::EtherAddr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070076 constexpr unsigned mtu = 150;
77
William A. Kennington III9c441fd2023-02-24 13:40:01 -080078 AllIntfInfo info{InterfaceInfo{.type = ARPHRD_ETHER,
79 .idx = 2,
William A. Kennington III13d665c2022-11-15 20:34:40 -080080 .flags = IFF_RUNNING,
81 .name = "test1",
82 .mac = mac,
83 .mtu = mtu}};
William A. Kennington III77848562022-10-26 17:32:57 -070084 MockEthernetInterface intf(bus, manager, info,
William A. Kennington IIId298f932022-10-17 14:31:38 -070085 "/xyz/openbmc_test/network"sv, config::Parser());
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070086
87 EXPECT_EQ(mtu, intf.mtu());
William A. Kennington IIIb7d6a1a2023-06-17 02:00:53 -070088 EXPECT_EQ(stdplus::toStr(mac), intf.macAddress());
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070089 EXPECT_TRUE(intf.linkUp());
90}
91
Ratan Gupta47722dc2017-05-26 18:32:23 +053092TEST_F(TestEthernetInterface, NoIPaddress)
93{
William A. Kennington III59e5b912022-11-02 02:49:46 -070094 EXPECT_TRUE(interface.addrs.empty());
Ratan Gupta47722dc2017-05-26 18:32:23 +053095}
96
97TEST_F(TestEthernetInterface, AddIPAddress)
98{
William A. Kennington III30f3ba22023-06-28 15:50:12 -070099 EXPECT_THROW(createIPObject(IP::Protocol::IPv4, "127.0.0.1", 16),
100 InvalidArgument);
101 EXPECT_THROW(createIPObject(IP::Protocol::IPv4, "127.0.0.1", 32),
102 InvalidArgument);
103 EXPECT_THROW(createIPObject(IP::Protocol::IPv4, "192.168.1.1", 0),
104 InvalidArgument);
105 EXPECT_THROW(createIPObject(IP::Protocol::IPv6, "::1", 64),
106 InvalidArgument);
107 EXPECT_THROW(createIPObject(IP::Protocol::IPv6, "::", 128),
108 InvalidArgument);
109 EXPECT_THROW(createIPObject(IP::Protocol::IPv6, "fe80::1", 0),
110 InvalidArgument);
111
William A. Kennington III59e5b912022-11-02 02:49:46 -0700112 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700113 EXPECT_THAT(interface.addrs, UnorderedElementsAre(Key(stdplus::SubnetAny(
114 stdplus::In4Addr{10, 10, 10, 10}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530115}
116
117TEST_F(TestEthernetInterface, AddMultipleAddress)
118{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700119 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
120 createIPObject(IP::Protocol::IPv4, "20.20.20.20", 16);
121 EXPECT_THAT(
122 interface.addrs,
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700123 UnorderedElementsAre(
124 Key(stdplus::SubnetAny(stdplus::In4Addr{10, 10, 10, 10}, 16)),
125 Key(stdplus::SubnetAny(stdplus::In4Addr{20, 20, 20, 20}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530126}
127
128TEST_F(TestEthernetInterface, DeleteIPAddress)
129{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700130 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
131 createIPObject(IP::Protocol::IPv4, "20.20.20.20", 16);
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700132 interface.addrs
133 .at(stdplus::SubnetAny(stdplus::In4Addr{10, 10, 10, 10}, 16))
134 ->delete_();
135 EXPECT_THAT(interface.addrs, UnorderedElementsAre(Key(stdplus::SubnetAny(
136 stdplus::In4Addr{20, 20, 20, 20}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530137}
138
139TEST_F(TestEthernetInterface, CheckObjectPath)
140{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700141 auto path = createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
142 EXPECT_EQ(path.parent_path(), "/xyz/openbmc_test/network/test0");
143 EXPECT_EQ(path.filename(), "10.10.10.10/16");
Ratan Gupta47722dc2017-05-26 18:32:23 +0530144}
145
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530146TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530147{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500148 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800149 EXPECT_CALL(manager.mockReload, schedule());
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530150 interface.staticNameServers(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", "DNS"));
Ratan Gupta09f61572017-08-23 16:53:52 +0530153}
154
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530155TEST_F(TestEthernetInterface, getDynamicNameServers)
156{
157 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
158 EXPECT_CALL(interface, getNameServerFromResolvd())
159 .WillRepeatedly(testing::Return(servers));
160 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
161}
162
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600163TEST_F(TestEthernetInterface, addStaticNTPServers)
Ratan Guptac9645fe2017-08-23 16:53:52 +0530164{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500165 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800166 EXPECT_CALL(manager.mockReload, schedule());
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600167 interface.staticNTPServers(servers);
William A. Kennington III5b179382022-11-15 15:23:26 -0800168 config::Parser parser((confDir / "00-bmc-test0.network").native());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700169 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP"));
Ratan Guptac9645fe2017-08-23 16:53:52 +0530170}
171
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600172TEST_F(TestEthernetInterface, addNTPServers)
173{
174 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
175 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
176 EXPECT_THROW(interface.ntpServers(servers), NotAllowed);
177}
178
179TEST_F(TestEthernetInterface, getNTPServers)
180{
181 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
182 setNtpServers();
183 EXPECT_EQ(getNtpServers(), servers);
184}
185
Ravi Tejaa5a09442020-07-17 00:57:33 -0500186TEST_F(TestEthernetInterface, addGateway)
187{
188 std::string gateway = "10.3.3.3";
189 interface.defaultGateway(gateway);
190 EXPECT_EQ(interface.defaultGateway(), gateway);
William A. Kennington IIIbbf5e9e2023-02-01 14:58:38 -0800191 EXPECT_THROW(interface.defaultGateway6("127.0.0.10"), InvalidArgument);
192 EXPECT_THROW(interface.defaultGateway6("0.0.0.0"), InvalidArgument);
193 EXPECT_THROW(interface.defaultGateway6("224.1.0.0"), InvalidArgument);
194 EXPECT_EQ(interface.defaultGateway(), gateway);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800195 interface.defaultGateway("");
196 EXPECT_EQ(interface.defaultGateway(), "");
William A. Kennington III9ca5c8e2022-12-14 14:40:52 -0800197 interface.defaultGateway("0.0.0.0");
198 EXPECT_EQ(interface.defaultGateway(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500199}
200
201TEST_F(TestEthernetInterface, addGateway6)
202{
William A. Kennington IIIbbf5e9e2023-02-01 14:58:38 -0800203 std::string gateway6 = "fe80::1";
Ravi Tejaa5a09442020-07-17 00:57:33 -0500204 interface.defaultGateway6(gateway6);
205 EXPECT_EQ(interface.defaultGateway6(), gateway6);
William A. Kennington IIIbbf5e9e2023-02-01 14:58:38 -0800206 EXPECT_THROW(interface.defaultGateway6("::1"), InvalidArgument);
207 EXPECT_EQ(interface.defaultGateway6(), gateway6);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800208 interface.defaultGateway6("");
209 EXPECT_EQ(interface.defaultGateway6(), "");
William A. Kennington III9ca5c8e2022-12-14 14:40:52 -0800210 interface.defaultGateway6("::");
211 EXPECT_EQ(interface.defaultGateway6(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500212}
213
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700214TEST_F(TestEthernetInterface, DHCPEnabled)
215{
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800216 EXPECT_CALL(manager.mockReload, schedule())
217 .WillRepeatedly(testing::Return());
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700218
219 using DHCPConf = EthernetInterfaceIntf::DHCPConf;
220 auto test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
221 EXPECT_EQ(conf, interface.dhcpEnabled());
222 EXPECT_EQ(dhcp4, interface.dhcp4());
223 EXPECT_EQ(dhcp6, interface.dhcp6());
224 EXPECT_EQ(ra, interface.ipv6AcceptRA());
225 };
226 test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
227
228 auto set_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
229 EXPECT_EQ(conf, interface.dhcpEnabled(conf));
230 test(conf, dhcp4, dhcp6, ra);
231 };
232 set_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
233 set_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
234 set_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
235 /*ra=*/true);
236 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
237 set_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
238 /*ra=*/true);
239 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
240
241 auto ind_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
242 EXPECT_EQ(dhcp4, interface.dhcp4(dhcp4));
243 EXPECT_EQ(dhcp6, interface.dhcp6(dhcp6));
244 EXPECT_EQ(ra, interface.ipv6AcceptRA(ra));
245 test(conf, dhcp4, dhcp6, ra);
246 };
247 ind_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
248 ind_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
249 ind_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
250 /*ra=*/true);
251 ind_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/false);
252 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
253 ind_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
254 /*ra=*/true);
255 ind_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/false);
256 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
257}
258
Gunnar Mills57d9c502018-09-14 14:42:34 -0500259} // namespace network
260} // namespace phosphor