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, |
| 93 | const std::string& gateway) |
| 94 | { |
| 95 | IP::Protocol addressType = IP::Protocol::IPv4; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 96 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 97 | return interface.generateObjectPath(addressType, ipaddress, subnetMask, |
| 98 | gateway); |
| 99 | } |
| 100 | |
| 101 | void createIPObject(IP::Protocol addressType, const std::string& ipaddress, |
| 102 | uint8_t subnetMask, const std::string& gateway) |
| 103 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 104 | interface.ip(addressType, ipaddress, subnetMask, gateway); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 105 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | TEST_F(TestEthernetInterface, NoIPaddress) |
| 109 | { |
| 110 | EXPECT_EQ(countIPObjects(), 0); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 111 | EXPECT_EQ(mac_address::toString(mac), interface.macAddress()); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | TEST_F(TestEthernetInterface, AddIPAddress) |
| 115 | { |
| 116 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 117 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 118 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | TEST_F(TestEthernetInterface, AddMultipleAddress) |
| 122 | { |
| 123 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 124 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 125 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 126 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
| 127 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST_F(TestEthernetInterface, DeleteIPAddress) |
| 131 | { |
| 132 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 133 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 134 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 135 | deleteIPObject("10.10.10.10"); |
| 136 | EXPECT_EQ(false, isIPObjectExist("10.10.10.10")); |
| 137 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | TEST_F(TestEthernetInterface, DeleteInvalidIPAddress) |
| 141 | { |
| 142 | EXPECT_EQ(false, deleteIPObject("10.10.10.10")); |
| 143 | } |
| 144 | |
| 145 | TEST_F(TestEthernetInterface, CheckObjectPath) |
| 146 | { |
| 147 | std::string ipaddress = "10.10.10.10"; |
| 148 | uint8_t prefix = 16; |
| 149 | std::string gateway = "10.10.10.1"; |
| 150 | |
| 151 | std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/"; |
| 152 | std::stringstream hexId; |
| 153 | |
| 154 | std::string hashString = ipaddress; |
| 155 | hashString += std::to_string(prefix); |
| 156 | hashString += gateway; |
| 157 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 158 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 159 | expectedObjectPath += hexId.str(); |
| 160 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 161 | EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway)); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 162 | } |
| 163 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 164 | TEST_F(TestEthernetInterface, addStaticNameServers) |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 165 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 166 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
Ratan Gupta | b400597 | 2019-09-19 06:19:16 +0530 | [diff] [blame] | 167 | EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 168 | interface.staticNameServers(servers); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 169 | fs::path filePath = confDir; |
| 170 | filePath /= "00-bmc-test0.network"; |
| 171 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 172 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 173 | config::ValueList values; |
| 174 | std::tie(rc, values) = parser.getValues("Network", "DNS"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 175 | EXPECT_EQ(servers, values); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 176 | } |
| 177 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 178 | TEST_F(TestEthernetInterface, addDynamicNameServers) |
| 179 | { |
| 180 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 181 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
| 182 | EXPECT_THROW(interface.nameservers(servers), NotAllowed); |
| 183 | } |
| 184 | |
| 185 | TEST_F(TestEthernetInterface, getDynamicNameServers) |
| 186 | { |
| 187 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
| 188 | EXPECT_CALL(interface, getNameServerFromResolvd()) |
| 189 | .WillRepeatedly(testing::Return(servers)); |
| 190 | EXPECT_EQ(interface.getNameServerFromResolvd(), servers); |
| 191 | } |
| 192 | |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 193 | TEST_F(TestEthernetInterface, addNTPServers) |
| 194 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 195 | ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"}; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 196 | EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 197 | interface.ntpServers(servers); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 198 | fs::path filePath = confDir; |
| 199 | filePath /= "00-bmc-test0.network"; |
| 200 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 201 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 202 | config::ValueList values; |
| 203 | std::tie(rc, values) = parser.getValues("Network", "NTP"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 204 | EXPECT_EQ(servers, values); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 205 | } |
| 206 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 207 | TEST_F(TestEthernetInterface, addGateway) |
| 208 | { |
| 209 | std::string gateway = "10.3.3.3"; |
| 210 | interface.defaultGateway(gateway); |
| 211 | EXPECT_EQ(interface.defaultGateway(), gateway); |
| 212 | } |
| 213 | |
| 214 | TEST_F(TestEthernetInterface, addGateway6) |
| 215 | { |
| 216 | std::string gateway6 = "ffff:ffff:ffff:fe80::1"; |
| 217 | interface.defaultGateway6(gateway6); |
| 218 | EXPECT_EQ(interface.defaultGateway6(), gateway6); |
| 219 | } |
| 220 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 221 | } // namespace network |
| 222 | } // namespace phosphor |