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); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 62 | return {bus, "/xyz/openbmc_test/network/test0", false, manager, true}; |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 65 | int countIPObjects() |
| 66 | { |
| 67 | return interface.getAddresses().size(); |
| 68 | } |
| 69 | |
| 70 | bool isIPObjectExist(const std::string& ipaddress) |
| 71 | { |
| 72 | auto address = interface.getAddresses().find(ipaddress); |
| 73 | if (address == interface.getAddresses().end()) |
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 | return false; |
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 | return true; |
| 78 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 79 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 80 | bool deleteIPObject(const std::string& ipaddress) |
| 81 | { |
| 82 | auto address = interface.getAddresses().find(ipaddress); |
| 83 | if (address == interface.getAddresses().end()) |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 84 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 85 | return false; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 86 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 87 | address->second->delete_(); |
| 88 | return true; |
| 89 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 90 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 91 | std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask, |
| 92 | const std::string& gateway) |
| 93 | { |
| 94 | IP::Protocol addressType = IP::Protocol::IPv4; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 95 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 96 | return interface.generateObjectPath(addressType, ipaddress, subnetMask, |
| 97 | gateway); |
| 98 | } |
| 99 | |
| 100 | void createIPObject(IP::Protocol addressType, const std::string& ipaddress, |
| 101 | uint8_t subnetMask, const std::string& gateway) |
| 102 | { |
| 103 | interface.iP(addressType, ipaddress, subnetMask, gateway); |
| 104 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | TEST_F(TestEthernetInterface, NoIPaddress) |
| 108 | { |
| 109 | EXPECT_EQ(countIPObjects(), 0); |
William A. Kennington III | cb64b99 | 2019-04-21 18:45:07 -0700 | [diff] [blame] | 110 | EXPECT_EQ(mac_address::toString(mac), interface.mACAddress()); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST_F(TestEthernetInterface, AddIPAddress) |
| 114 | { |
| 115 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 116 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 117 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | TEST_F(TestEthernetInterface, AddMultipleAddress) |
| 121 | { |
| 122 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 123 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 124 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 125 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
| 126 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | TEST_F(TestEthernetInterface, DeleteIPAddress) |
| 130 | { |
| 131 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 132 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 133 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 134 | deleteIPObject("10.10.10.10"); |
| 135 | EXPECT_EQ(false, isIPObjectExist("10.10.10.10")); |
| 136 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST_F(TestEthernetInterface, DeleteInvalidIPAddress) |
| 140 | { |
| 141 | EXPECT_EQ(false, deleteIPObject("10.10.10.10")); |
| 142 | } |
| 143 | |
| 144 | TEST_F(TestEthernetInterface, CheckObjectPath) |
| 145 | { |
| 146 | std::string ipaddress = "10.10.10.10"; |
| 147 | uint8_t prefix = 16; |
| 148 | std::string gateway = "10.10.10.1"; |
| 149 | |
| 150 | std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/"; |
| 151 | std::stringstream hexId; |
| 152 | |
| 153 | std::string hashString = ipaddress; |
| 154 | hashString += std::to_string(prefix); |
| 155 | hashString += gateway; |
| 156 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 157 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 158 | expectedObjectPath += hexId.str(); |
| 159 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 160 | EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway)); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 161 | } |
| 162 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 163 | TEST_F(TestEthernetInterface, addStaticNameServers) |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 164 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 165 | 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] | 166 | EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 167 | interface.staticNameServers(servers); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 168 | fs::path filePath = confDir; |
| 169 | filePath /= "00-bmc-test0.network"; |
| 170 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 171 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 172 | config::ValueList values; |
| 173 | std::tie(rc, values) = parser.getValues("Network", "DNS"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 174 | EXPECT_EQ(servers, values); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 175 | } |
| 176 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 177 | TEST_F(TestEthernetInterface, addDynamicNameServers) |
| 178 | { |
| 179 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 180 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
| 181 | EXPECT_THROW(interface.nameservers(servers), NotAllowed); |
| 182 | } |
| 183 | |
| 184 | TEST_F(TestEthernetInterface, getDynamicNameServers) |
| 185 | { |
| 186 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
| 187 | EXPECT_CALL(interface, getNameServerFromResolvd()) |
| 188 | .WillRepeatedly(testing::Return(servers)); |
| 189 | EXPECT_EQ(interface.getNameServerFromResolvd(), servers); |
| 190 | } |
| 191 | |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 192 | TEST_F(TestEthernetInterface, addNTPServers) |
| 193 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 194 | 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] | 195 | EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 196 | interface.nTPServers(servers); |
| 197 | fs::path filePath = confDir; |
| 198 | filePath /= "00-bmc-test0.network"; |
| 199 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 200 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 201 | config::ValueList values; |
| 202 | std::tie(rc, values) = parser.getValues("Network", "NTP"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 203 | EXPECT_EQ(servers, values); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 204 | } |
| 205 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 206 | } // namespace network |
| 207 | } // namespace phosphor |