blob: e9f3973981b7a23e0b8a371cdfd78e191f4151c7 [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>
Ratan Gupta255d5142017-08-10 09:02:08 +05307
Gunnar Mills57d9c502018-09-14 14:42:34 -05008#include <sdbusplus/bus.hpp>
William A. Kennington III84bfe672022-07-13 14:15:30 -07009#include <stdplus/gtest/tmp.hpp>
William A. Kennington III991a8e82022-10-11 15:02:47 -070010#include <string_view>
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060011#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050012
13#include <gtest/gtest.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +053014
15namespace phosphor
16{
17namespace network
18{
19
William A. Kennington III991a8e82022-10-11 15:02:47 -070020using std::literals::string_view_literals::operator""sv;
William A. Kennington III59e5b912022-11-02 02:49:46 -070021using testing::Key;
22using testing::UnorderedElementsAre;
William A. Kennington III991a8e82022-10-11 15:02:47 -070023
William A. Kennington III84bfe672022-07-13 14:15:30 -070024class TestEthernetInterface : public stdplus::gtest::TestWithTmp
Ratan Gupta47722dc2017-05-26 18:32:23 +053025{
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 public:
Patrick Williamsc38b0712022-07-22 19:26:54 -050027 sdbusplus::bus_t bus;
William A. Kennington III5b179382022-11-15 15:23:26 -080028 std::filesystem::path confDir;
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080029 TestManager manager;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053030 MockEthernetInterface interface;
Gunnar Mills57d9c502018-09-14 14:42:34 -050031 TestEthernetInterface() :
William A. Kennington III84bfe672022-07-13 14:15:30 -070032 bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
33 manager(bus, "/xyz/openbmc_test/network", confDir),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070034 interface(makeInterface(bus, manager))
Ratan Gupta47722dc2017-05-26 18:32:23 +053035
Gunnar Mills57d9c502018-09-14 14:42:34 -050036 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053038
Patrick Williamsc38b0712022-07-22 19:26:54 -050039 static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus,
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080040 TestManager& manager)
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070041 {
William A. Kennington III13d665c2022-11-15 20:34:40 -080042 AllIntfInfo info{InterfaceInfo{.idx = 1, .flags = 0, .name = "test0"}};
William A. Kennington III77848562022-10-26 17:32:57 -070043 return {bus, manager, info, "/xyz/openbmc_test/network"sv,
44 config::Parser()};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070045 }
46
William A. Kennington III59e5b912022-11-02 02:49:46 -070047 auto createIPObject(IP::Protocol addressType, const std::string& ipaddress,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070048 uint8_t subnetMask)
Gunnar Mills57d9c502018-09-14 14:42:34 -050049 {
William A. Kennington III59e5b912022-11-02 02:49:46 -070050 return interface.ip(addressType, ipaddress, subnetMask, "");
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 }
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060052
53 void setNtpServers()
54 {
55 ServerList ntpServers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
56 interface.EthernetInterfaceIntf::ntpServers(ntpServers);
57 }
58
59 ServerList getNtpServers()
60 {
61 return interface.EthernetInterfaceIntf::ntpServers();
62 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053063};
64
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070065TEST_F(TestEthernetInterface, Fields)
66{
67 EXPECT_EQ(0, interface.mtu());
68 EXPECT_EQ("", interface.macAddress());
69 EXPECT_FALSE(interface.linkUp());
70
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070071 constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
72 constexpr unsigned mtu = 150;
73
William A. Kennington III13d665c2022-11-15 20:34:40 -080074 AllIntfInfo info{InterfaceInfo{.idx = 2,
75 .flags = IFF_RUNNING,
76 .name = "test1",
77 .mac = mac,
78 .mtu = mtu}};
William A. Kennington III77848562022-10-26 17:32:57 -070079 MockEthernetInterface intf(bus, manager, info,
William A. Kennington IIId298f932022-10-17 14:31:38 -070080 "/xyz/openbmc_test/network"sv, config::Parser());
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070081
82 EXPECT_EQ(mtu, intf.mtu());
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -070083 EXPECT_EQ(std::to_string(mac), intf.macAddress());
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070084 EXPECT_TRUE(intf.linkUp());
85}
86
Ratan Gupta47722dc2017-05-26 18:32:23 +053087TEST_F(TestEthernetInterface, NoIPaddress)
88{
William A. Kennington III59e5b912022-11-02 02:49:46 -070089 EXPECT_TRUE(interface.addrs.empty());
Ratan Gupta47722dc2017-05-26 18:32:23 +053090}
91
92TEST_F(TestEthernetInterface, AddIPAddress)
93{
William A. Kennington III59e5b912022-11-02 02:49:46 -070094 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
95 EXPECT_THAT(interface.addrs, UnorderedElementsAre(Key(
96 IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +053097}
98
99TEST_F(TestEthernetInterface, AddMultipleAddress)
100{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700101 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
102 createIPObject(IP::Protocol::IPv4, "20.20.20.20", 16);
103 EXPECT_THAT(
104 interface.addrs,
105 UnorderedElementsAre(Key(IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16)),
106 Key(IfAddr(in_addr{htonl(0x14141414)}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530107}
108
109TEST_F(TestEthernetInterface, DeleteIPAddress)
110{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700111 createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
112 createIPObject(IP::Protocol::IPv4, "20.20.20.20", 16);
113 interface.addrs.at(IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16))->delete_();
114 EXPECT_THAT(interface.addrs, UnorderedElementsAre(Key(
115 IfAddr(in_addr{htonl(0x14141414)}, 16))));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530116}
117
118TEST_F(TestEthernetInterface, CheckObjectPath)
119{
William A. Kennington III59e5b912022-11-02 02:49:46 -0700120 auto path = createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16);
121 EXPECT_EQ(path.parent_path(), "/xyz/openbmc_test/network/test0");
122 EXPECT_EQ(path.filename(), "10.10.10.10/16");
Ratan Gupta47722dc2017-05-26 18:32:23 +0530123}
124
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530125TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530126{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500127 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800128 EXPECT_CALL(manager.mockReload, schedule());
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530129 interface.staticNameServers(servers);
William A. Kennington III5b179382022-11-15 15:23:26 -0800130 config::Parser parser((confDir / "00-bmc-test0.network").native());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700131 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "DNS"));
Ratan Gupta09f61572017-08-23 16:53:52 +0530132}
133
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530134TEST_F(TestEthernetInterface, getDynamicNameServers)
135{
136 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
137 EXPECT_CALL(interface, getNameServerFromResolvd())
138 .WillRepeatedly(testing::Return(servers));
139 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
140}
141
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600142TEST_F(TestEthernetInterface, addStaticNTPServers)
Ratan Guptac9645fe2017-08-23 16:53:52 +0530143{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500144 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800145 EXPECT_CALL(manager.mockReload, schedule());
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600146 interface.staticNTPServers(servers);
William A. Kennington III5b179382022-11-15 15:23:26 -0800147 config::Parser parser((confDir / "00-bmc-test0.network").native());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700148 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP"));
Ratan Guptac9645fe2017-08-23 16:53:52 +0530149}
150
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600151TEST_F(TestEthernetInterface, addNTPServers)
152{
153 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
154 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
155 EXPECT_THROW(interface.ntpServers(servers), NotAllowed);
156}
157
158TEST_F(TestEthernetInterface, getNTPServers)
159{
160 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
161 setNtpServers();
162 EXPECT_EQ(getNtpServers(), servers);
163}
164
Ravi Tejaa5a09442020-07-17 00:57:33 -0500165TEST_F(TestEthernetInterface, addGateway)
166{
167 std::string gateway = "10.3.3.3";
168 interface.defaultGateway(gateway);
169 EXPECT_EQ(interface.defaultGateway(), gateway);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800170 interface.defaultGateway("");
171 EXPECT_EQ(interface.defaultGateway(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500172}
173
174TEST_F(TestEthernetInterface, addGateway6)
175{
176 std::string gateway6 = "ffff:ffff:ffff:fe80::1";
177 interface.defaultGateway6(gateway6);
178 EXPECT_EQ(interface.defaultGateway6(), gateway6);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800179 interface.defaultGateway6("");
180 EXPECT_EQ(interface.defaultGateway6(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500181}
182
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700183TEST_F(TestEthernetInterface, DHCPEnabled)
184{
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800185 EXPECT_CALL(manager.mockReload, schedule())
186 .WillRepeatedly(testing::Return());
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700187
188 using DHCPConf = EthernetInterfaceIntf::DHCPConf;
189 auto test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
190 EXPECT_EQ(conf, interface.dhcpEnabled());
191 EXPECT_EQ(dhcp4, interface.dhcp4());
192 EXPECT_EQ(dhcp6, interface.dhcp6());
193 EXPECT_EQ(ra, interface.ipv6AcceptRA());
194 };
195 test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
196
197 auto set_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
198 EXPECT_EQ(conf, interface.dhcpEnabled(conf));
199 test(conf, dhcp4, dhcp6, ra);
200 };
201 set_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
202 set_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
203 set_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
204 /*ra=*/true);
205 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
206 set_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
207 /*ra=*/true);
208 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
209
210 auto ind_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) {
211 EXPECT_EQ(dhcp4, interface.dhcp4(dhcp4));
212 EXPECT_EQ(dhcp6, interface.dhcp6(dhcp6));
213 EXPECT_EQ(ra, interface.ipv6AcceptRA(ra));
214 test(conf, dhcp4, dhcp6, ra);
215 };
216 ind_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false);
217 ind_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false);
218 ind_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false,
219 /*ra=*/true);
220 ind_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/false);
221 set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true);
222 ind_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false,
223 /*ra=*/true);
224 ind_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/false);
225 set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
226}
227
Gunnar Mills57d9c502018-09-14 14:42:34 -0500228} // namespace network
229} // namespace phosphor