blob: 9e611a612d783f96210c187f622a6edf0ec39ff5 [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"
Ratan Gupta47722dc2017-05-26 18:32:23 +05305
Ratan Gupta255d5142017-08-10 09:02:08 +05306#include <arpa/inet.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +05307#include <net/if.h>
8#include <netinet/in.h>
Ratan Gupta255d5142017-08-10 09:02:08 +05309#include <stdlib.h>
10
Ratan Gupta47722dc2017-05-26 18:32:23 +053011#include <exception>
Ratan Guptac9645fe2017-08-23 16:53:52 +053012#include <fstream>
Gunnar Mills57d9c502018-09-14 14:42:34 -050013#include <sdbusplus/bus.hpp>
14
15#include <gtest/gtest.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +053016
17namespace phosphor
18{
19namespace network
20{
21
22class TestEthernetInterface : public testing::Test
23{
Gunnar Mills57d9c502018-09-14 14:42:34 -050024 public:
25 sdbusplus::bus::bus bus;
Ratan Gupta35297172018-11-28 18:40:16 +053026 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050027 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 Gupta47722dc2017-05-26 18:32:23 +053033
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 {
35 setConfDir();
36 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053037
Gunnar Mills57d9c502018-09-14 14:42:34 -050038 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 Gupta47722dc2017-05-26 18:32:23 +053048 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050049 fs::remove_all(confDir);
Ratan Gupta47722dc2017-05-26 18:32:23 +053050 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053052
Gunnar Mills57d9c502018-09-14 14:42:34 -050053 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 Gupta255d5142017-08-10 09:02:08 +053062 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050063 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053064 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050065 return true;
66 }
Ratan Gupta255d5142017-08-10 09:02:08 +053067
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 bool deleteIPObject(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 address->second->delete_();
76 return true;
77 }
Ratan Gupta255d5142017-08-10 09:02:08 +053078
Gunnar Mills57d9c502018-09-14 14:42:34 -050079 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
80 const std::string& gateway)
81 {
82 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053083
Gunnar Mills57d9c502018-09-14 14:42:34 -050084 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 Gupta47722dc2017-05-26 18:32:23 +0530111 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500112 actualServers += line;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530113 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500114 EXPECT_EQ(expectedServers, actualServers);
115 }
Ratan Gupta47722dc2017-05-26 18:32:23 +0530116};
117
118TEST_F(TestEthernetInterface, NoIPaddress)
119{
120 EXPECT_EQ(countIPObjects(), 0);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530121}
122
123TEST_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 Gupta47722dc2017-05-26 18:32:23 +0530128}
129
130TEST_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 Gupta47722dc2017-05-26 18:32:23 +0530137}
138
139TEST_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 Gupta47722dc2017-05-26 18:32:23 +0530147}
148
149TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
150{
151 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
152}
153
154TEST_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 Mills57d9c502018-09-14 14:42:34 -0500167 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530168 expectedObjectPath += hexId.str();
169
Gunnar Mills57d9c502018-09-14 14:42:34 -0500170 EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530171}
172
Ratan Gupta09f61572017-08-23 16:53:52 +0530173TEST_F(TestEthernetInterface, addNameServers)
174{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500175 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
Ratan Gupta09f61572017-08-23 16:53:52 +0530176 interface.nameservers(servers);
177 fs::path filePath = confDir;
178 filePath /= "00-bmc-test0.network";
179 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530180 config::ReturnCode rc = config::ReturnCode::SUCCESS;
181 config::ValueList values;
182 std::tie(rc, values) = parser.getValues("Network", "DNS");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500183 EXPECT_EQ(servers, values);
Ratan Gupta09f61572017-08-23 16:53:52 +0530184
185 validateResolvFile(values);
186}
187
Ratan Guptac9645fe2017-08-23 16:53:52 +0530188TEST_F(TestEthernetInterface, addNTPServers)
189{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500190 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
Ratan Gupta35297172018-11-28 18:40:16 +0530191 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530192 interface.nTPServers(servers);
193 fs::path filePath = confDir;
194 filePath /= "00-bmc-test0.network";
195 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530196 config::ReturnCode rc = config::ReturnCode::SUCCESS;
197 config::ValueList values;
198 std::tie(rc, values) = parser.getValues("Network", "NTP");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500199 EXPECT_EQ(servers, values);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530200}
201
Gunnar Mills57d9c502018-09-14 14:42:34 -0500202} // namespace network
203} // namespace phosphor