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