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" |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 5 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 6 | #include <arpa/inet.h> |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 7 | #include <net/if.h> |
| 8 | #include <netinet/in.h> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 9 | #include <stdlib.h> |
| 10 | |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 11 | #include <exception> |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 12 | #include <fstream> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 13 | #include <sdbusplus/bus.hpp> |
| 14 | |
| 15 | #include <gtest/gtest.h> |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace network |
| 20 | { |
| 21 | |
| 22 | class TestEthernetInterface : public testing::Test |
| 23 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 24 | public: |
| 25 | sdbusplus::bus::bus bus; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 26 | MockManager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 27 | EthernetInterface interface; |
| 28 | std::string confDir; |
| 29 | TestEthernetInterface() : |
| 30 | bus(sdbusplus::bus::new_default()), |
| 31 | manager(bus, "/xyz/openbmc_test/network", "/tmp/"), |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame^] | 32 | interface(makeInterface(bus, manager)) |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 33 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 34 | { |
| 35 | setConfDir(); |
| 36 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 37 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 38 | void setConfDir() |
| 39 | { |
| 40 | char tmp[] = "/tmp/EthernetInterface.XXXXXX"; |
| 41 | confDir = mkdtemp(tmp); |
| 42 | manager.setConfDir(confDir); |
| 43 | } |
| 44 | |
| 45 | ~TestEthernetInterface() |
| 46 | { |
| 47 | if (confDir != "") |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 48 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 49 | fs::remove_all(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 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 52 | |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame^] | 53 | static EthernetInterface makeInterface(sdbusplus::bus::bus& bus, |
| 54 | MockManager& manager) |
| 55 | { |
| 56 | mock_addIF("test0", 1); |
| 57 | return {bus, "/xyz/openbmc_test/network/test0", false, manager}; |
| 58 | } |
| 59 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 60 | int countIPObjects() |
| 61 | { |
| 62 | return interface.getAddresses().size(); |
| 63 | } |
| 64 | |
| 65 | bool isIPObjectExist(const std::string& ipaddress) |
| 66 | { |
| 67 | auto address = interface.getAddresses().find(ipaddress); |
| 68 | if (address == interface.getAddresses().end()) |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 69 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 70 | return false; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 71 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 72 | return true; |
| 73 | } |
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 | bool deleteIPObject(const std::string& ipaddress) |
| 76 | { |
| 77 | auto address = interface.getAddresses().find(ipaddress); |
| 78 | if (address == interface.getAddresses().end()) |
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 | return false; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 81 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 82 | address->second->delete_(); |
| 83 | return true; |
| 84 | } |
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 | std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask, |
| 87 | const std::string& gateway) |
| 88 | { |
| 89 | IP::Protocol addressType = IP::Protocol::IPv4; |
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 | return interface.generateObjectPath(addressType, ipaddress, subnetMask, |
| 92 | gateway); |
| 93 | } |
| 94 | |
| 95 | void createIPObject(IP::Protocol addressType, const std::string& ipaddress, |
| 96 | uint8_t subnetMask, const std::string& gateway) |
| 97 | { |
| 98 | interface.iP(addressType, ipaddress, subnetMask, gateway); |
| 99 | } |
| 100 | |
| 101 | // Validates if the DNS entries have been correctly processed |
| 102 | void validateResolvFile(ServerList values) |
| 103 | { |
| 104 | // Check whether the entries has been written to resolv.conf |
| 105 | fs::path resolvFile = confDir; |
| 106 | resolvFile /= "resolv.conf"; |
| 107 | |
| 108 | // Passed in "value" is what is read from the config file |
| 109 | interface.writeDNSEntries(values, resolvFile); |
| 110 | std::string expectedServers = |
| 111 | "### Generated manually via dbus settings ###"; |
| 112 | expectedServers += |
| 113 | "nameserver 9.1.1.1nameserver 9.2.2.2nameserver 9.3.3.3"; |
| 114 | |
| 115 | std::string actualServers{}; |
| 116 | std::fstream stream(resolvFile.string().c_str(), std::fstream::in); |
| 117 | for (std::string line; std::getline(stream, line);) |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 118 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 119 | actualServers += line; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 120 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 121 | EXPECT_EQ(expectedServers, actualServers); |
| 122 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | TEST_F(TestEthernetInterface, NoIPaddress) |
| 126 | { |
| 127 | EXPECT_EQ(countIPObjects(), 0); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST_F(TestEthernetInterface, AddIPAddress) |
| 131 | { |
| 132 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 133 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 134 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | TEST_F(TestEthernetInterface, AddMultipleAddress) |
| 138 | { |
| 139 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 140 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 141 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 142 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
| 143 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | TEST_F(TestEthernetInterface, DeleteIPAddress) |
| 147 | { |
| 148 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 149 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 150 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 151 | deleteIPObject("10.10.10.10"); |
| 152 | EXPECT_EQ(false, isIPObjectExist("10.10.10.10")); |
| 153 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | TEST_F(TestEthernetInterface, DeleteInvalidIPAddress) |
| 157 | { |
| 158 | EXPECT_EQ(false, deleteIPObject("10.10.10.10")); |
| 159 | } |
| 160 | |
| 161 | TEST_F(TestEthernetInterface, CheckObjectPath) |
| 162 | { |
| 163 | std::string ipaddress = "10.10.10.10"; |
| 164 | uint8_t prefix = 16; |
| 165 | std::string gateway = "10.10.10.1"; |
| 166 | |
| 167 | std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/"; |
| 168 | std::stringstream hexId; |
| 169 | |
| 170 | std::string hashString = ipaddress; |
| 171 | hashString += std::to_string(prefix); |
| 172 | hashString += gateway; |
| 173 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 174 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 175 | expectedObjectPath += hexId.str(); |
| 176 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 177 | EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway)); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 178 | } |
| 179 | |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 180 | TEST_F(TestEthernetInterface, addNameServers) |
| 181 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 182 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 183 | interface.nameservers(servers); |
| 184 | fs::path filePath = confDir; |
| 185 | filePath /= "00-bmc-test0.network"; |
| 186 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 187 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 188 | config::ValueList values; |
| 189 | std::tie(rc, values) = parser.getValues("Network", "DNS"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 190 | EXPECT_EQ(servers, values); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 191 | |
| 192 | validateResolvFile(values); |
| 193 | } |
| 194 | |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 195 | TEST_F(TestEthernetInterface, addNTPServers) |
| 196 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 197 | 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] | 198 | EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 199 | interface.nTPServers(servers); |
| 200 | fs::path filePath = confDir; |
| 201 | filePath /= "00-bmc-test0.network"; |
| 202 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 203 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 204 | config::ValueList values; |
| 205 | std::tie(rc, values) = parser.getValues("Network", "NTP"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 206 | EXPECT_EQ(servers, values); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 207 | } |
| 208 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 209 | } // namespace network |
| 210 | } // namespace phosphor |