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