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 | |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 12 | #include <exception> |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 13 | #include <fstream> |
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> |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 16 | #include <xyz/openbmc_project/Common/error.hpp> |
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 | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 25 | class TestEthernetInterface : public stdplus::gtest::TestWithTmp |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 26 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 27 | public: |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame^] | 28 | sdbusplus::bus_t bus; |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 29 | std::string confDir; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 30 | MockManager manager; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 31 | MockEthernetInterface interface; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 32 | TestEthernetInterface() : |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 33 | bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()), |
| 34 | manager(bus, "/xyz/openbmc_test/network", confDir), |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 35 | interface(makeInterface(bus, manager)) |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 36 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 37 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 38 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 39 | |
William A. Kennington III | cb64b99 | 2019-04-21 18:45:07 -0700 | [diff] [blame] | 40 | static constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
| 41 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame^] | 42 | static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus, |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 43 | MockManager& manager) |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 44 | { |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 45 | mock_clear(); |
William A. Kennington III | cb64b99 | 2019-04-21 18:45:07 -0700 | [diff] [blame] | 46 | mock_addIF("test0", 1, mac); |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 47 | return {bus, "/xyz/openbmc_test/network/test0", |
| 48 | EthernetInterface::DHCPConf::none, manager, true}; |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 51 | int countIPObjects() |
| 52 | { |
| 53 | return interface.getAddresses().size(); |
| 54 | } |
| 55 | |
| 56 | bool isIPObjectExist(const std::string& ipaddress) |
| 57 | { |
| 58 | auto address = interface.getAddresses().find(ipaddress); |
| 59 | if (address == interface.getAddresses().end()) |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 60 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 61 | return false; |
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 true; |
| 64 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 65 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 66 | bool deleteIPObject(const std::string& ipaddress) |
| 67 | { |
| 68 | auto address = interface.getAddresses().find(ipaddress); |
| 69 | if (address == interface.getAddresses().end()) |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 70 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 71 | return false; |
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 | address->second->delete_(); |
| 74 | return true; |
| 75 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 76 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 77 | std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask, |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 78 | const std::string& gateway, |
| 79 | IP::AddressOrigin origin) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 80 | { |
| 81 | IP::Protocol addressType = IP::Protocol::IPv4; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 82 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 83 | return interface.generateObjectPath(addressType, ipaddress, subnetMask, |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 84 | gateway, origin); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void createIPObject(IP::Protocol addressType, const std::string& ipaddress, |
| 88 | uint8_t subnetMask, const std::string& gateway) |
| 89 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 90 | interface.ip(addressType, ipaddress, subnetMask, gateway); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 91 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | TEST_F(TestEthernetInterface, NoIPaddress) |
| 95 | { |
| 96 | EXPECT_EQ(countIPObjects(), 0); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 97 | EXPECT_EQ(mac_address::toString(mac), interface.macAddress()); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | TEST_F(TestEthernetInterface, AddIPAddress) |
| 101 | { |
| 102 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 103 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 104 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | TEST_F(TestEthernetInterface, AddMultipleAddress) |
| 108 | { |
| 109 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 110 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 111 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 112 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
| 113 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST_F(TestEthernetInterface, DeleteIPAddress) |
| 117 | { |
| 118 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 119 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 120 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 121 | deleteIPObject("10.10.10.10"); |
| 122 | EXPECT_EQ(false, isIPObjectExist("10.10.10.10")); |
| 123 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | TEST_F(TestEthernetInterface, DeleteInvalidIPAddress) |
| 127 | { |
| 128 | EXPECT_EQ(false, deleteIPObject("10.10.10.10")); |
| 129 | } |
| 130 | |
| 131 | TEST_F(TestEthernetInterface, CheckObjectPath) |
| 132 | { |
| 133 | std::string ipaddress = "10.10.10.10"; |
| 134 | uint8_t prefix = 16; |
| 135 | std::string gateway = "10.10.10.1"; |
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 | |
| 138 | std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/"; |
| 139 | std::stringstream hexId; |
| 140 | |
| 141 | std::string hashString = ipaddress; |
| 142 | hashString += std::to_string(prefix); |
| 143 | hashString += gateway; |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 144 | hashString += convertForMessage(origin); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 145 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 146 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 147 | expectedObjectPath += hexId.str(); |
| 148 | |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 149 | EXPECT_EQ(expectedObjectPath, |
| 150 | getObjectPath(ipaddress, prefix, gateway, origin)); |
| 151 | |
| 152 | origin = IP::AddressOrigin::DHCP; |
| 153 | EXPECT_NE(expectedObjectPath, |
| 154 | getObjectPath(ipaddress, prefix, gateway, origin)); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 155 | } |
| 156 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 157 | TEST_F(TestEthernetInterface, addStaticNameServers) |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 158 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 159 | 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] | 160 | EXPECT_CALL(manager, reloadConfigs()); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 161 | interface.staticNameServers(servers); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 162 | fs::path filePath = confDir; |
| 163 | filePath /= "00-bmc-test0.network"; |
| 164 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 165 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 166 | config::ValueList values; |
| 167 | std::tie(rc, values) = parser.getValues("Network", "DNS"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 168 | EXPECT_EQ(servers, values); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 169 | } |
| 170 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 171 | TEST_F(TestEthernetInterface, addDynamicNameServers) |
| 172 | { |
| 173 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 174 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
| 175 | EXPECT_THROW(interface.nameservers(servers), NotAllowed); |
| 176 | } |
| 177 | |
| 178 | TEST_F(TestEthernetInterface, getDynamicNameServers) |
| 179 | { |
| 180 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
| 181 | EXPECT_CALL(interface, getNameServerFromResolvd()) |
| 182 | .WillRepeatedly(testing::Return(servers)); |
| 183 | EXPECT_EQ(interface.getNameServerFromResolvd(), servers); |
| 184 | } |
| 185 | |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 186 | TEST_F(TestEthernetInterface, addNTPServers) |
| 187 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 188 | 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] | 189 | EXPECT_CALL(manager, reloadConfigs()); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 190 | interface.ntpServers(servers); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 191 | fs::path filePath = confDir; |
| 192 | filePath /= "00-bmc-test0.network"; |
| 193 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 194 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 195 | config::ValueList values; |
| 196 | std::tie(rc, values) = parser.getValues("Network", "NTP"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 197 | EXPECT_EQ(servers, values); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 198 | } |
| 199 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 200 | TEST_F(TestEthernetInterface, addGateway) |
| 201 | { |
| 202 | std::string gateway = "10.3.3.3"; |
| 203 | interface.defaultGateway(gateway); |
| 204 | EXPECT_EQ(interface.defaultGateway(), gateway); |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 205 | interface.defaultGateway(""); |
| 206 | EXPECT_EQ(interface.defaultGateway(), ""); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | TEST_F(TestEthernetInterface, addGateway6) |
| 210 | { |
| 211 | std::string gateway6 = "ffff:ffff:ffff:fe80::1"; |
| 212 | interface.defaultGateway6(gateway6); |
| 213 | EXPECT_EQ(interface.defaultGateway6(), gateway6); |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 214 | interface.defaultGateway6(""); |
| 215 | EXPECT_EQ(interface.defaultGateway6(), ""); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 218 | } // namespace network |
| 219 | } // namespace phosphor |