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