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/"), |
| 32 | interface(bus, "/xyz/openbmc_test/network/test0", false, 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 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 53 | int countIPObjects() |
| 54 | { |
| 55 | return interface.getAddresses().size(); |
| 56 | } |
| 57 | |
| 58 | bool isIPObjectExist(const std::string& ipaddress) |
| 59 | { |
| 60 | auto address = interface.getAddresses().find(ipaddress); |
| 61 | if (address == interface.getAddresses().end()) |
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 false; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 64 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 65 | return true; |
| 66 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 67 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 68 | bool deleteIPObject(const std::string& ipaddress) |
| 69 | { |
| 70 | auto address = interface.getAddresses().find(ipaddress); |
| 71 | if (address == interface.getAddresses().end()) |
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 | return false; |
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 | address->second->delete_(); |
| 76 | return true; |
| 77 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 78 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 79 | std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask, |
| 80 | const std::string& gateway) |
| 81 | { |
| 82 | IP::Protocol addressType = IP::Protocol::IPv4; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 83 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 84 | return interface.generateObjectPath(addressType, ipaddress, subnetMask, |
| 85 | gateway); |
| 86 | } |
| 87 | |
| 88 | void createIPObject(IP::Protocol addressType, const std::string& ipaddress, |
| 89 | uint8_t subnetMask, const std::string& gateway) |
| 90 | { |
| 91 | interface.iP(addressType, ipaddress, subnetMask, gateway); |
| 92 | } |
| 93 | |
| 94 | // Validates if the DNS entries have been correctly processed |
| 95 | void validateResolvFile(ServerList values) |
| 96 | { |
| 97 | // Check whether the entries has been written to resolv.conf |
| 98 | fs::path resolvFile = confDir; |
| 99 | resolvFile /= "resolv.conf"; |
| 100 | |
| 101 | // Passed in "value" is what is read from the config file |
| 102 | interface.writeDNSEntries(values, resolvFile); |
| 103 | std::string expectedServers = |
| 104 | "### Generated manually via dbus settings ###"; |
| 105 | expectedServers += |
| 106 | "nameserver 9.1.1.1nameserver 9.2.2.2nameserver 9.3.3.3"; |
| 107 | |
| 108 | std::string actualServers{}; |
| 109 | std::fstream stream(resolvFile.string().c_str(), std::fstream::in); |
| 110 | for (std::string line; std::getline(stream, line);) |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 111 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 112 | actualServers += line; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 113 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 114 | EXPECT_EQ(expectedServers, actualServers); |
| 115 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | TEST_F(TestEthernetInterface, NoIPaddress) |
| 119 | { |
| 120 | EXPECT_EQ(countIPObjects(), 0); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(TestEthernetInterface, AddIPAddress) |
| 124 | { |
| 125 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 126 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 127 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST_F(TestEthernetInterface, AddMultipleAddress) |
| 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 | EXPECT_EQ(true, 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, DeleteIPAddress) |
| 140 | { |
| 141 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 142 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 143 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 144 | deleteIPObject("10.10.10.10"); |
| 145 | EXPECT_EQ(false, isIPObjectExist("10.10.10.10")); |
| 146 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | TEST_F(TestEthernetInterface, DeleteInvalidIPAddress) |
| 150 | { |
| 151 | EXPECT_EQ(false, deleteIPObject("10.10.10.10")); |
| 152 | } |
| 153 | |
| 154 | TEST_F(TestEthernetInterface, CheckObjectPath) |
| 155 | { |
| 156 | std::string ipaddress = "10.10.10.10"; |
| 157 | uint8_t prefix = 16; |
| 158 | std::string gateway = "10.10.10.1"; |
| 159 | |
| 160 | std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/"; |
| 161 | std::stringstream hexId; |
| 162 | |
| 163 | std::string hashString = ipaddress; |
| 164 | hashString += std::to_string(prefix); |
| 165 | hashString += gateway; |
| 166 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 167 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 168 | expectedObjectPath += hexId.str(); |
| 169 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 170 | EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway)); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 171 | } |
| 172 | |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 173 | TEST_F(TestEthernetInterface, addNameServers) |
| 174 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 175 | 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] | 176 | interface.nameservers(servers); |
| 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 | validateResolvFile(values); |
| 186 | } |
| 187 | |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 188 | TEST_F(TestEthernetInterface, addNTPServers) |
| 189 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 190 | 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] | 191 | EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 192 | interface.nTPServers(servers); |
| 193 | fs::path filePath = confDir; |
| 194 | filePath /= "00-bmc-test0.network"; |
| 195 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 196 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 197 | config::ValueList values; |
| 198 | std::tie(rc, values) = parser.getValues("Network", "NTP"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 199 | EXPECT_EQ(servers, values); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 200 | } |
| 201 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 202 | } // namespace network |
| 203 | } // namespace phosphor |