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