blob: a07542351eac12e35ea12feda8fb35a7e5bea5b7 [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/"),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070032 interface(makeInterface(bus, 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
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070053 static EthernetInterface makeInterface(sdbusplus::bus::bus& bus,
54 MockManager& manager)
55 {
56 mock_addIF("test0", 1);
57 return {bus, "/xyz/openbmc_test/network/test0", false, manager};
58 }
59
Gunnar Mills57d9c502018-09-14 14:42:34 -050060 int countIPObjects()
61 {
62 return interface.getAddresses().size();
63 }
64
65 bool isIPObjectExist(const std::string& ipaddress)
66 {
67 auto address = interface.getAddresses().find(ipaddress);
68 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053069 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050070 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053071 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050072 return true;
73 }
Ratan Gupta255d5142017-08-10 09:02:08 +053074
Gunnar Mills57d9c502018-09-14 14:42:34 -050075 bool deleteIPObject(const std::string& ipaddress)
76 {
77 auto address = interface.getAddresses().find(ipaddress);
78 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053079 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050080 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053081 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050082 address->second->delete_();
83 return true;
84 }
Ratan Gupta255d5142017-08-10 09:02:08 +053085
Gunnar Mills57d9c502018-09-14 14:42:34 -050086 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
87 const std::string& gateway)
88 {
89 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053090
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 return interface.generateObjectPath(addressType, ipaddress, subnetMask,
92 gateway);
93 }
94
95 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
96 uint8_t subnetMask, const std::string& gateway)
97 {
98 interface.iP(addressType, ipaddress, subnetMask, gateway);
99 }
100
101 // Validates if the DNS entries have been correctly processed
102 void validateResolvFile(ServerList values)
103 {
104 // Check whether the entries has been written to resolv.conf
105 fs::path resolvFile = confDir;
106 resolvFile /= "resolv.conf";
107
108 // Passed in "value" is what is read from the config file
109 interface.writeDNSEntries(values, resolvFile);
110 std::string expectedServers =
111 "### Generated manually via dbus settings ###";
112 expectedServers +=
113 "nameserver 9.1.1.1nameserver 9.2.2.2nameserver 9.3.3.3";
114
115 std::string actualServers{};
116 std::fstream stream(resolvFile.string().c_str(), std::fstream::in);
117 for (std::string line; std::getline(stream, line);)
Ratan Gupta47722dc2017-05-26 18:32:23 +0530118 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500119 actualServers += line;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530120 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500121 EXPECT_EQ(expectedServers, actualServers);
122 }
Ratan Gupta47722dc2017-05-26 18:32:23 +0530123};
124
125TEST_F(TestEthernetInterface, NoIPaddress)
126{
127 EXPECT_EQ(countIPObjects(), 0);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530128}
129
130TEST_F(TestEthernetInterface, AddIPAddress)
131{
132 IP::Protocol addressType = IP::Protocol::IPv4;
133 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
134 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530135}
136
137TEST_F(TestEthernetInterface, AddMultipleAddress)
138{
139 IP::Protocol addressType = IP::Protocol::IPv4;
140 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
141 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
142 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
143 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530144}
145
146TEST_F(TestEthernetInterface, DeleteIPAddress)
147{
148 IP::Protocol addressType = IP::Protocol::IPv4;
149 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
150 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
151 deleteIPObject("10.10.10.10");
152 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
153 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530154}
155
156TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
157{
158 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
159}
160
161TEST_F(TestEthernetInterface, CheckObjectPath)
162{
163 std::string ipaddress = "10.10.10.10";
164 uint8_t prefix = 16;
165 std::string gateway = "10.10.10.1";
166
167 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
168 std::stringstream hexId;
169
170 std::string hashString = ipaddress;
171 hashString += std::to_string(prefix);
172 hashString += gateway;
173
Gunnar Mills57d9c502018-09-14 14:42:34 -0500174 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530175 expectedObjectPath += hexId.str();
176
Gunnar Mills57d9c502018-09-14 14:42:34 -0500177 EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530178}
179
Ratan Gupta09f61572017-08-23 16:53:52 +0530180TEST_F(TestEthernetInterface, addNameServers)
181{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500182 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
Ratan Gupta09f61572017-08-23 16:53:52 +0530183 interface.nameservers(servers);
184 fs::path filePath = confDir;
185 filePath /= "00-bmc-test0.network";
186 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530187 config::ReturnCode rc = config::ReturnCode::SUCCESS;
188 config::ValueList values;
189 std::tie(rc, values) = parser.getValues("Network", "DNS");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500190 EXPECT_EQ(servers, values);
Ratan Gupta09f61572017-08-23 16:53:52 +0530191
192 validateResolvFile(values);
193}
194
Ratan Guptac9645fe2017-08-23 16:53:52 +0530195TEST_F(TestEthernetInterface, addNTPServers)
196{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500197 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
Ratan Gupta35297172018-11-28 18:40:16 +0530198 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530199 interface.nTPServers(servers);
200 fs::path filePath = confDir;
201 filePath /= "00-bmc-test0.network";
202 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530203 config::ReturnCode rc = config::ReturnCode::SUCCESS;
204 config::ValueList values;
205 std::tie(rc, values) = parser.getValues("Network", "NTP");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500206 EXPECT_EQ(servers, values);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530207}
208
Gunnar Mills57d9c502018-09-14 14:42:34 -0500209} // namespace network
210} // namespace phosphor