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" |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 3 | #include "mock_network_manager.hpp" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 4 | #include "mock_syscall.hpp" |
William A. Kennington III | cb64b99 | 2019-04-21 18:45:07 -0700 | [diff] [blame] | 5 | #include "util.hpp" |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 6 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 8 | #include <net/if.h> |
| 9 | #include <netinet/in.h> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 12 | #include <charconv> |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 13 | #include <exception> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 14 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 15 | #include <stdplus/gtest/tmp.hpp> |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 16 | #include <string_view> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 17 | |
| 18 | #include <gtest/gtest.h> |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 19 | |
| 20 | namespace phosphor |
| 21 | { |
| 22 | namespace network |
| 23 | { |
| 24 | |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 25 | using std::literals::string_view_literals::operator""sv; |
| 26 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 27 | class TestEthernetInterface : public stdplus::gtest::TestWithTmp |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 28 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 29 | public: |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 30 | sdbusplus::bus_t bus; |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 31 | std::string confDir; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 32 | MockManager manager; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 33 | MockEthernetInterface interface; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 34 | TestEthernetInterface() : |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 35 | bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()), |
| 36 | manager(bus, "/xyz/openbmc_test/network", confDir), |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 37 | interface(makeInterface(bus, manager)) |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 38 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 39 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 40 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 41 | |
William A. Kennington III | cb64b99 | 2019-04-21 18:45:07 -0700 | [diff] [blame] | 42 | static constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
| 43 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 44 | static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus, |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 45 | MockManager& manager) |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 46 | { |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 47 | mock_clear(); |
William A. Kennington III | cb64b99 | 2019-04-21 18:45:07 -0700 | [diff] [blame] | 48 | mock_addIF("test0", 1, mac); |
William A. Kennington III | 0caf221 | 2022-08-18 18:15:51 -0700 | [diff] [blame] | 49 | return {bus, "/xyz/openbmc_test/network/test0", config::Parser(), |
| 50 | manager, true}; |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 53 | int countIPObjects() |
| 54 | { |
| 55 | return interface.getAddresses().size(); |
| 56 | } |
| 57 | |
| 58 | bool isIPObjectExist(const std::string& ipaddress) |
| 59 | { |
| 60 | auto address = interface.getAddresses().find(ipaddress); |
| 61 | if (address == interface.getAddresses().end()) |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 62 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 63 | return false; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 64 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 65 | return true; |
| 66 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 67 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 68 | bool deleteIPObject(const std::string& ipaddress) |
| 69 | { |
| 70 | auto address = interface.getAddresses().find(ipaddress); |
| 71 | if (address == interface.getAddresses().end()) |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 72 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 73 | return false; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 74 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 75 | address->second->delete_(); |
| 76 | return true; |
| 77 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 78 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 79 | std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask, |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 80 | IP::AddressOrigin origin) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 81 | { |
| 82 | IP::Protocol addressType = IP::Protocol::IPv4; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 83 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 84 | return interface.generateObjectPath(addressType, ipaddress, subnetMask, |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 85 | origin); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void createIPObject(IP::Protocol addressType, const std::string& ipaddress, |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 89 | uint8_t subnetMask) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 90 | { |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 91 | interface.ip(addressType, ipaddress, subnetMask, ""); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 92 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | TEST_F(TestEthernetInterface, NoIPaddress) |
| 96 | { |
| 97 | EXPECT_EQ(countIPObjects(), 0); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 98 | EXPECT_EQ(mac_address::toString(mac), interface.macAddress()); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | TEST_F(TestEthernetInterface, AddIPAddress) |
| 102 | { |
| 103 | IP::Protocol addressType = IP::Protocol::IPv4; |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 104 | createIPObject(addressType, "10.10.10.10", 16); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 105 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TEST_F(TestEthernetInterface, AddMultipleAddress) |
| 109 | { |
| 110 | IP::Protocol addressType = IP::Protocol::IPv4; |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 111 | createIPObject(addressType, "10.10.10.10", 16); |
| 112 | createIPObject(addressType, "20.20.20.20", 16); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 113 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
| 114 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST_F(TestEthernetInterface, DeleteIPAddress) |
| 118 | { |
| 119 | IP::Protocol addressType = IP::Protocol::IPv4; |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 120 | createIPObject(addressType, "10.10.10.10", 16); |
| 121 | createIPObject(addressType, "20.20.20.20", 16); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 122 | deleteIPObject("10.10.10.10"); |
| 123 | EXPECT_EQ(false, isIPObjectExist("10.10.10.10")); |
| 124 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | TEST_F(TestEthernetInterface, DeleteInvalidIPAddress) |
| 128 | { |
| 129 | EXPECT_EQ(false, deleteIPObject("10.10.10.10")); |
| 130 | } |
| 131 | |
| 132 | TEST_F(TestEthernetInterface, CheckObjectPath) |
| 133 | { |
| 134 | std::string ipaddress = "10.10.10.10"; |
| 135 | uint8_t prefix = 16; |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 136 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 137 | |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 138 | auto path = getObjectPath(ipaddress, prefix, origin); |
| 139 | auto pathsv = std::string_view(path); |
| 140 | constexpr auto expectedPrefix = "/xyz/openbmc_test/network/test0/ipv4/"sv; |
| 141 | EXPECT_TRUE(pathsv.starts_with(expectedPrefix)); |
| 142 | pathsv.remove_prefix(expectedPrefix.size()); |
| 143 | uint32_t val; |
| 144 | auto [ptr, res] = std::from_chars(pathsv.begin(), pathsv.end(), val, 16); |
| 145 | EXPECT_EQ(res, std::errc()); |
| 146 | EXPECT_EQ(ptr, pathsv.end()); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 147 | |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 148 | EXPECT_EQ(path, getObjectPath(ipaddress, prefix, origin)); |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 149 | origin = IP::AddressOrigin::DHCP; |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 150 | EXPECT_NE(path, getObjectPath(ipaddress, prefix, origin)); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 151 | } |
| 152 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 153 | TEST_F(TestEthernetInterface, addStaticNameServers) |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 154 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 155 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 156 | EXPECT_CALL(manager, reloadConfigs()); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 157 | interface.staticNameServers(servers); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 158 | fs::path filePath = confDir; |
| 159 | filePath /= "00-bmc-test0.network"; |
| 160 | config::Parser parser(filePath.string()); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 161 | EXPECT_EQ(servers, parser.map.getValueStrings("Network", "DNS")); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 162 | } |
| 163 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 164 | TEST_F(TestEthernetInterface, getDynamicNameServers) |
| 165 | { |
| 166 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
| 167 | EXPECT_CALL(interface, getNameServerFromResolvd()) |
| 168 | .WillRepeatedly(testing::Return(servers)); |
| 169 | EXPECT_EQ(interface.getNameServerFromResolvd(), servers); |
| 170 | } |
| 171 | |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 172 | TEST_F(TestEthernetInterface, addNTPServers) |
| 173 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 174 | ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"}; |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 175 | EXPECT_CALL(manager, reloadConfigs()); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 176 | interface.ntpServers(servers); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 177 | fs::path filePath = confDir; |
| 178 | filePath /= "00-bmc-test0.network"; |
| 179 | config::Parser parser(filePath.string()); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 180 | EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP")); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 181 | } |
| 182 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 183 | TEST_F(TestEthernetInterface, addGateway) |
| 184 | { |
| 185 | std::string gateway = "10.3.3.3"; |
| 186 | interface.defaultGateway(gateway); |
| 187 | EXPECT_EQ(interface.defaultGateway(), gateway); |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 188 | interface.defaultGateway(""); |
| 189 | EXPECT_EQ(interface.defaultGateway(), ""); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | TEST_F(TestEthernetInterface, addGateway6) |
| 193 | { |
| 194 | std::string gateway6 = "ffff:ffff:ffff:fe80::1"; |
| 195 | interface.defaultGateway6(gateway6); |
| 196 | EXPECT_EQ(interface.defaultGateway6(), gateway6); |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 197 | interface.defaultGateway6(""); |
| 198 | EXPECT_EQ(interface.defaultGateway6(), ""); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 199 | } |
| 200 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 201 | TEST_F(TestEthernetInterface, DHCPEnabled) |
| 202 | { |
| 203 | EXPECT_CALL(manager, reloadConfigs()).WillRepeatedly(testing::Return()); |
| 204 | |
| 205 | using DHCPConf = EthernetInterfaceIntf::DHCPConf; |
| 206 | auto test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) { |
| 207 | EXPECT_EQ(conf, interface.dhcpEnabled()); |
| 208 | EXPECT_EQ(dhcp4, interface.dhcp4()); |
| 209 | EXPECT_EQ(dhcp6, interface.dhcp6()); |
| 210 | EXPECT_EQ(ra, interface.ipv6AcceptRA()); |
| 211 | }; |
| 212 | test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true); |
| 213 | |
| 214 | auto set_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) { |
| 215 | EXPECT_EQ(conf, interface.dhcpEnabled(conf)); |
| 216 | test(conf, dhcp4, dhcp6, ra); |
| 217 | }; |
| 218 | set_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false); |
| 219 | set_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false); |
| 220 | set_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false, |
| 221 | /*ra=*/true); |
| 222 | set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true); |
| 223 | set_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false, |
| 224 | /*ra=*/true); |
| 225 | set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true); |
| 226 | |
| 227 | auto ind_test = [&](DHCPConf conf, bool dhcp4, bool dhcp6, bool ra) { |
| 228 | EXPECT_EQ(dhcp4, interface.dhcp4(dhcp4)); |
| 229 | EXPECT_EQ(dhcp6, interface.dhcp6(dhcp6)); |
| 230 | EXPECT_EQ(ra, interface.ipv6AcceptRA(ra)); |
| 231 | test(conf, dhcp4, dhcp6, ra); |
| 232 | }; |
| 233 | ind_test(DHCPConf::none, /*dhcp4=*/false, /*dhcp6=*/false, /*ra=*/false); |
| 234 | ind_test(DHCPConf::v4, /*dhcp4=*/true, /*dhcp6=*/false, /*ra=*/false); |
| 235 | ind_test(DHCPConf::v6stateless, /*dhcp4=*/false, /*dhcp6=*/false, |
| 236 | /*ra=*/true); |
| 237 | ind_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/false); |
| 238 | set_test(DHCPConf::v6, /*dhcp4=*/false, /*dhcp6=*/true, /*ra=*/true); |
| 239 | ind_test(DHCPConf::v4v6stateless, /*dhcp4=*/true, /*dhcp6=*/false, |
| 240 | /*ra=*/true); |
| 241 | ind_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/false); |
| 242 | set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true); |
| 243 | } |
| 244 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 245 | } // namespace network |
| 246 | } // namespace phosphor |