blob: c25bba90cc13603783f933e8ddbb3de3c202ebfa [file] [log] [blame]
Ratan Guptac9645fe2017-08-23 16:53:52 +05301#include "config_parser.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05302#include "ipaddress.hpp"
Ratan Gupta35297172018-11-28 18:40:16 +05303#include "mock_network_manager.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05004#include "mock_syscall.hpp"
William A. Kennington IIIcb64b992019-04-21 18:45:07 -07005#include "util.hpp"
Ratan Gupta47722dc2017-05-26 18:32:23 +05306
Ratan Gupta255d5142017-08-10 09:02:08 +05307#include <arpa/inet.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +05308#include <net/if.h>
9#include <netinet/in.h>
Ratan Gupta255d5142017-08-10 09:02:08 +053010#include <stdlib.h>
11
Ratan Gupta47722dc2017-05-26 18:32:23 +053012#include <exception>
Ratan Guptac9645fe2017-08-23 16:53:52 +053013#include <fstream>
Gunnar Mills57d9c502018-09-14 14:42:34 -050014#include <sdbusplus/bus.hpp>
15
16#include <gtest/gtest.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +053017
18namespace phosphor
19{
20namespace network
21{
22
23class TestEthernetInterface : public testing::Test
24{
Gunnar Mills57d9c502018-09-14 14:42:34 -050025 public:
26 sdbusplus::bus::bus bus;
Ratan Gupta35297172018-11-28 18:40:16 +053027 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050028 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 IIIebb1ad02019-04-21 18:02:49 -070033 interface(makeInterface(bus, manager))
Ratan Gupta47722dc2017-05-26 18:32:23 +053034
Gunnar Mills57d9c502018-09-14 14:42:34 -050035 {
36 setConfDir();
37 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053038
Gunnar Mills57d9c502018-09-14 14:42:34 -050039 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 Gupta47722dc2017-05-26 18:32:23 +053049 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050050 fs::remove_all(confDir);
Ratan Gupta47722dc2017-05-26 18:32:23 +053051 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050052 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053053
William A. Kennington IIIcb64b992019-04-21 18:45:07 -070054 static constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
55
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070056 static EthernetInterface makeInterface(sdbusplus::bus::bus& bus,
57 MockManager& manager)
58 {
William A. Kennington IIIcb64b992019-04-21 18:45:07 -070059 mock_addIF("test0", 1, mac);
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070060 return {bus, "/xyz/openbmc_test/network/test0", false, manager};
61 }
62
Gunnar Mills57d9c502018-09-14 14:42:34 -050063 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 Gupta255d5142017-08-10 09:02:08 +053072 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053074 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050075 return true;
76 }
Ratan Gupta255d5142017-08-10 09:02:08 +053077
Gunnar Mills57d9c502018-09-14 14:42:34 -050078 bool deleteIPObject(const std::string& ipaddress)
79 {
80 auto address = interface.getAddresses().find(ipaddress);
81 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053082 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050083 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053084 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050085 address->second->delete_();
86 return true;
87 }
Ratan Gupta255d5142017-08-10 09:02:08 +053088
Gunnar Mills57d9c502018-09-14 14:42:34 -050089 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
90 const std::string& gateway)
91 {
92 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053093
Gunnar Mills57d9c502018-09-14 14:42:34 -050094 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 Gupta47722dc2017-05-26 18:32:23 +0530121 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500122 actualServers += line;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530123 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500124 EXPECT_EQ(expectedServers, actualServers);
125 }
Ratan Gupta47722dc2017-05-26 18:32:23 +0530126};
127
128TEST_F(TestEthernetInterface, NoIPaddress)
129{
130 EXPECT_EQ(countIPObjects(), 0);
William A. Kennington IIIcb64b992019-04-21 18:45:07 -0700131 EXPECT_EQ(mac_address::toString(mac), interface.mACAddress());
Ratan Gupta47722dc2017-05-26 18:32:23 +0530132}
133
134TEST_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 Gupta47722dc2017-05-26 18:32:23 +0530139}
140
141TEST_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 Gupta47722dc2017-05-26 18:32:23 +0530148}
149
150TEST_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 Gupta47722dc2017-05-26 18:32:23 +0530158}
159
160TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
161{
162 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
163}
164
165TEST_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 Mills57d9c502018-09-14 14:42:34 -0500178 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530179 expectedObjectPath += hexId.str();
180
Gunnar Mills57d9c502018-09-14 14:42:34 -0500181 EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530182}
183
Ratan Gupta09f61572017-08-23 16:53:52 +0530184TEST_F(TestEthernetInterface, addNameServers)
185{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500186 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
Ratan Gupta09f61572017-08-23 16:53:52 +0530187 interface.nameservers(servers);
188 fs::path filePath = confDir;
189 filePath /= "00-bmc-test0.network";
190 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530191 config::ReturnCode rc = config::ReturnCode::SUCCESS;
192 config::ValueList values;
193 std::tie(rc, values) = parser.getValues("Network", "DNS");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500194 EXPECT_EQ(servers, values);
Ratan Gupta09f61572017-08-23 16:53:52 +0530195
196 validateResolvFile(values);
197}
198
Ratan Guptac9645fe2017-08-23 16:53:52 +0530199TEST_F(TestEthernetInterface, addNTPServers)
200{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500201 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
Ratan Gupta35297172018-11-28 18:40:16 +0530202 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530203 interface.nTPServers(servers);
204 fs::path filePath = confDir;
205 filePath /= "00-bmc-test0.network";
206 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530207 config::ReturnCode rc = config::ReturnCode::SUCCESS;
208 config::ValueList values;
209 std::tie(rc, values) = parser.getValues("Network", "NTP");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500210 EXPECT_EQ(servers, values);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530211}
212
Gunnar Mills57d9c502018-09-14 14:42:34 -0500213} // namespace network
214} // namespace phosphor