Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 1 | #include "config_parser.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 2 | #include "ipaddress.hpp" |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 3 | #include "mock_ethernet_interface.hpp" |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 4 | #include "test_network_manager.hpp" |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 5 | |
| 6 | #include <net/if.h> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 7 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 9 | #include <stdplus/gtest/tmp.hpp> |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 10 | #include <string_view> |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Common/error.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 12 | |
| 13 | #include <gtest/gtest.h> |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace network |
| 18 | { |
| 19 | |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 20 | using std::literals::string_view_literals::operator""sv; |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 21 | using testing::Key; |
| 22 | using testing::UnorderedElementsAre; |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 23 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 24 | class TestEthernetInterface : public stdplus::gtest::TestWithTmp |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 25 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 26 | public: |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame^] | 27 | stdplus::Pinned<sdbusplus::bus_t> bus; |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 28 | std::filesystem::path confDir; |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 29 | TestManager manager; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 30 | MockEthernetInterface interface; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 31 | TestEthernetInterface() : |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 32 | bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()), |
| 33 | manager(bus, "/xyz/openbmc_test/network", confDir), |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 34 | interface(makeInterface(bus, manager)) |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 35 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 36 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 37 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 38 | |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame^] | 39 | static MockEthernetInterface |
| 40 | makeInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus, |
| 41 | TestManager& manager) |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 42 | { |
William A. Kennington III | 13d665c | 2022-11-15 20:34:40 -0800 | [diff] [blame] | 43 | AllIntfInfo info{InterfaceInfo{.idx = 1, .flags = 0, .name = "test0"}}; |
William A. Kennington III | 7784856 | 2022-10-26 17:32:57 -0700 | [diff] [blame] | 44 | return {bus, manager, info, "/xyz/openbmc_test/network"sv, |
| 45 | config::Parser()}; |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 46 | } |
| 47 | |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 48 | auto createIPObject(IP::Protocol addressType, const std::string& ipaddress, |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 49 | uint8_t subnetMask) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 50 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 51 | return interface.ip(addressType, ipaddress, subnetMask, ""); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 52 | } |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 53 | |
| 54 | void setNtpServers() |
| 55 | { |
| 56 | ServerList ntpServers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"}; |
| 57 | interface.EthernetInterfaceIntf::ntpServers(ntpServers); |
| 58 | } |
| 59 | |
| 60 | ServerList getNtpServers() |
| 61 | { |
| 62 | return interface.EthernetInterfaceIntf::ntpServers(); |
| 63 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 64 | }; |
| 65 | |
William A. Kennington III | 9ecb90e | 2022-10-14 03:12:43 -0700 | [diff] [blame] | 66 | TEST_F(TestEthernetInterface, Fields) |
| 67 | { |
| 68 | EXPECT_EQ(0, interface.mtu()); |
| 69 | EXPECT_EQ("", interface.macAddress()); |
| 70 | EXPECT_FALSE(interface.linkUp()); |
| 71 | |
William A. Kennington III | 9ecb90e | 2022-10-14 03:12:43 -0700 | [diff] [blame] | 72 | constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
| 73 | constexpr unsigned mtu = 150; |
| 74 | |
William A. Kennington III | 13d665c | 2022-11-15 20:34:40 -0800 | [diff] [blame] | 75 | AllIntfInfo info{InterfaceInfo{.idx = 2, |
| 76 | .flags = IFF_RUNNING, |
| 77 | .name = "test1", |
| 78 | .mac = mac, |
| 79 | .mtu = mtu}}; |
William A. Kennington III | 7784856 | 2022-10-26 17:32:57 -0700 | [diff] [blame] | 80 | MockEthernetInterface intf(bus, manager, info, |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 81 | "/xyz/openbmc_test/network"sv, config::Parser()); |
William A. Kennington III | 9ecb90e | 2022-10-14 03:12:43 -0700 | [diff] [blame] | 82 | |
| 83 | EXPECT_EQ(mtu, intf.mtu()); |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 84 | EXPECT_EQ(std::to_string(mac), intf.macAddress()); |
William A. Kennington III | 9ecb90e | 2022-10-14 03:12:43 -0700 | [diff] [blame] | 85 | EXPECT_TRUE(intf.linkUp()); |
| 86 | } |
| 87 | |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 88 | TEST_F(TestEthernetInterface, NoIPaddress) |
| 89 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 90 | EXPECT_TRUE(interface.addrs.empty()); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TEST_F(TestEthernetInterface, AddIPAddress) |
| 94 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 95 | createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16); |
| 96 | EXPECT_THAT(interface.addrs, UnorderedElementsAre(Key( |
| 97 | IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16)))); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | TEST_F(TestEthernetInterface, AddMultipleAddress) |
| 101 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 102 | createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16); |
| 103 | createIPObject(IP::Protocol::IPv4, "20.20.20.20", 16); |
| 104 | EXPECT_THAT( |
| 105 | interface.addrs, |
| 106 | UnorderedElementsAre(Key(IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16)), |
| 107 | Key(IfAddr(in_addr{htonl(0x14141414)}, 16)))); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TEST_F(TestEthernetInterface, DeleteIPAddress) |
| 111 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 112 | createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16); |
| 113 | createIPObject(IP::Protocol::IPv4, "20.20.20.20", 16); |
| 114 | interface.addrs.at(IfAddr(in_addr{htonl(0x0a0a0a0a)}, 16))->delete_(); |
| 115 | EXPECT_THAT(interface.addrs, UnorderedElementsAre(Key( |
| 116 | IfAddr(in_addr{htonl(0x14141414)}, 16)))); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST_F(TestEthernetInterface, CheckObjectPath) |
| 120 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 121 | auto path = createIPObject(IP::Protocol::IPv4, "10.10.10.10", 16); |
| 122 | EXPECT_EQ(path.parent_path(), "/xyz/openbmc_test/network/test0"); |
| 123 | EXPECT_EQ(path.filename(), "10.10.10.10/16"); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 124 | } |
| 125 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 126 | TEST_F(TestEthernetInterface, addStaticNameServers) |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 127 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 128 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 129 | EXPECT_CALL(manager.mockReload, schedule()); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 130 | interface.staticNameServers(servers); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 131 | config::Parser parser((confDir / "00-bmc-test0.network").native()); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 132 | EXPECT_EQ(servers, parser.map.getValueStrings("Network", "DNS")); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 133 | } |
| 134 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 135 | TEST_F(TestEthernetInterface, getDynamicNameServers) |
| 136 | { |
| 137 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
| 138 | EXPECT_CALL(interface, getNameServerFromResolvd()) |
| 139 | .WillRepeatedly(testing::Return(servers)); |
| 140 | EXPECT_EQ(interface.getNameServerFromResolvd(), servers); |
| 141 | } |
| 142 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 143 | TEST_F(TestEthernetInterface, addStaticNTPServers) |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 144 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 145 | ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"}; |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 146 | EXPECT_CALL(manager.mockReload, schedule()); |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 147 | interface.staticNTPServers(servers); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 148 | config::Parser parser((confDir / "00-bmc-test0.network").native()); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 149 | EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP")); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 150 | } |
| 151 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 152 | TEST_F(TestEthernetInterface, addNTPServers) |
| 153 | { |
| 154 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 155 | ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"}; |
| 156 | EXPECT_THROW(interface.ntpServers(servers), NotAllowed); |
| 157 | } |
| 158 | |
| 159 | TEST_F(TestEthernetInterface, getNTPServers) |
| 160 | { |
| 161 | ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"}; |
| 162 | setNtpServers(); |
| 163 | EXPECT_EQ(getNtpServers(), servers); |
| 164 | } |
| 165 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 166 | TEST_F(TestEthernetInterface, addGateway) |
| 167 | { |
| 168 | std::string gateway = "10.3.3.3"; |
| 169 | interface.defaultGateway(gateway); |
| 170 | EXPECT_EQ(interface.defaultGateway(), gateway); |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 171 | interface.defaultGateway(""); |
| 172 | EXPECT_EQ(interface.defaultGateway(), ""); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | TEST_F(TestEthernetInterface, addGateway6) |
| 176 | { |
| 177 | std::string gateway6 = "ffff:ffff:ffff:fe80::1"; |
| 178 | interface.defaultGateway6(gateway6); |
| 179 | EXPECT_EQ(interface.defaultGateway6(), gateway6); |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 180 | interface.defaultGateway6(""); |
| 181 | EXPECT_EQ(interface.defaultGateway6(), ""); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 182 | } |
| 183 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 184 | TEST_F(TestEthernetInterface, DHCPEnabled) |
| 185 | { |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 186 | EXPECT_CALL(manager.mockReload, schedule()) |
| 187 | .WillRepeatedly(testing::Return()); |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 188 | |
| 189 | using DHCPConf = EthernetInterfaceIntf::DHCPConf; |
| 190 | auto test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) { |
| 191 | EXPECT_EQ(conf, interface.dhcpEnabled()); |
| 192 | EXPECT_EQ(dhcp4, interface.dhcp4()); |
| 193 | EXPECT_EQ(dhcp6, interface.dhcp6()); |
| 194 | EXPECT_EQ(ra, interface.ipv6AcceptRA()); |
| 195 | }; |
| 196 | test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true); |
| 197 | |
| 198 | auto set_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) { |
| 199 | EXPECT_EQ(conf, interface.dhcpEnabled(conf)); |
| 200 | test(conf, dhcp4, dhcp6, ra); |
| 201 | }; |
| 202 | set_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false); |
| 203 | set_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false); |
| 204 | set_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false, |
| 205 | /*ra=*/true); |
| 206 | set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true); |
| 207 | set_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false, |
| 208 | /*ra=*/true); |
| 209 | set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true); |
| 210 | |
| 211 | auto ind_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) { |
| 212 | EXPECT_EQ(dhcp4, interface.dhcp4(dhcp4)); |
| 213 | EXPECT_EQ(dhcp6, interface.dhcp6(dhcp6)); |
| 214 | EXPECT_EQ(ra, interface.ipv6AcceptRA(ra)); |
| 215 | test(conf, dhcp4, dhcp6, ra); |
| 216 | }; |
| 217 | ind_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false); |
| 218 | ind_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false); |
| 219 | ind_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false, |
| 220 | /*ra=*/true); |
| 221 | ind_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/false); |
| 222 | set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true); |
| 223 | ind_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false, |
| 224 | /*ra=*/true); |
| 225 | ind_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/false); |
| 226 | set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true); |
| 227 | } |
| 228 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 229 | } // namespace network |
| 230 | } // namespace phosphor |