blob: 5ea7f7afa5e6dd48d71d8546591a10ead069dcde [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 III862275a2019-04-22 20:37:08 -070059 mock_clear();
William A. Kennington IIIcb64b992019-04-21 18:45:07 -070060 mock_addIF("test0", 1, mac);
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070061 return {bus, "/xyz/openbmc_test/network/test0", false, manager};
62 }
63
Gunnar Mills57d9c502018-09-14 14:42:34 -050064 int countIPObjects()
65 {
66 return interface.getAddresses().size();
67 }
68
69 bool isIPObjectExist(const std::string& ipaddress)
70 {
71 auto address = interface.getAddresses().find(ipaddress);
72 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053073 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050074 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053075 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050076 return true;
77 }
Ratan Gupta255d5142017-08-10 09:02:08 +053078
Gunnar Mills57d9c502018-09-14 14:42:34 -050079 bool deleteIPObject(const std::string& ipaddress)
80 {
81 auto address = interface.getAddresses().find(ipaddress);
82 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053083 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050084 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053085 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050086 address->second->delete_();
87 return true;
88 }
Ratan Gupta255d5142017-08-10 09:02:08 +053089
Gunnar Mills57d9c502018-09-14 14:42:34 -050090 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
91 const std::string& gateway)
92 {
93 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053094
Gunnar Mills57d9c502018-09-14 14:42:34 -050095 return interface.generateObjectPath(addressType, ipaddress, subnetMask,
96 gateway);
97 }
98
99 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
100 uint8_t subnetMask, const std::string& gateway)
101 {
102 interface.iP(addressType, ipaddress, subnetMask, gateway);
103 }
104
105 // Validates if the DNS entries have been correctly processed
106 void validateResolvFile(ServerList values)
107 {
108 // Check whether the entries has been written to resolv.conf
109 fs::path resolvFile = confDir;
110 resolvFile /= "resolv.conf";
111
112 // Passed in "value" is what is read from the config file
113 interface.writeDNSEntries(values, resolvFile);
114 std::string expectedServers =
115 "### Generated manually via dbus settings ###";
116 expectedServers +=
117 "nameserver 9.1.1.1nameserver 9.2.2.2nameserver 9.3.3.3";
118
119 std::string actualServers{};
120 std::fstream stream(resolvFile.string().c_str(), std::fstream::in);
121 for (std::string line; std::getline(stream, line);)
Ratan Gupta47722dc2017-05-26 18:32:23 +0530122 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500123 actualServers += line;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530124 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500125 EXPECT_EQ(expectedServers, actualServers);
126 }
Ratan Gupta47722dc2017-05-26 18:32:23 +0530127};
128
129TEST_F(TestEthernetInterface, NoIPaddress)
130{
131 EXPECT_EQ(countIPObjects(), 0);
William A. Kennington IIIcb64b992019-04-21 18:45:07 -0700132 EXPECT_EQ(mac_address::toString(mac), interface.mACAddress());
Ratan Gupta47722dc2017-05-26 18:32:23 +0530133}
134
135TEST_F(TestEthernetInterface, AddIPAddress)
136{
137 IP::Protocol addressType = IP::Protocol::IPv4;
138 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
139 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530140}
141
142TEST_F(TestEthernetInterface, AddMultipleAddress)
143{
144 IP::Protocol addressType = IP::Protocol::IPv4;
145 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
146 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
147 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
148 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530149}
150
151TEST_F(TestEthernetInterface, DeleteIPAddress)
152{
153 IP::Protocol addressType = IP::Protocol::IPv4;
154 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
155 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
156 deleteIPObject("10.10.10.10");
157 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
158 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530159}
160
161TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
162{
163 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
164}
165
166TEST_F(TestEthernetInterface, CheckObjectPath)
167{
168 std::string ipaddress = "10.10.10.10";
169 uint8_t prefix = 16;
170 std::string gateway = "10.10.10.1";
171
172 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
173 std::stringstream hexId;
174
175 std::string hashString = ipaddress;
176 hashString += std::to_string(prefix);
177 hashString += gateway;
178
Gunnar Mills57d9c502018-09-14 14:42:34 -0500179 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530180 expectedObjectPath += hexId.str();
181
Gunnar Mills57d9c502018-09-14 14:42:34 -0500182 EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530183}
184
Ratan Gupta09f61572017-08-23 16:53:52 +0530185TEST_F(TestEthernetInterface, addNameServers)
186{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500187 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
Ratan Gupta09f61572017-08-23 16:53:52 +0530188 interface.nameservers(servers);
189 fs::path filePath = confDir;
190 filePath /= "00-bmc-test0.network";
191 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530192 config::ReturnCode rc = config::ReturnCode::SUCCESS;
193 config::ValueList values;
194 std::tie(rc, values) = parser.getValues("Network", "DNS");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500195 EXPECT_EQ(servers, values);
Ratan Gupta09f61572017-08-23 16:53:52 +0530196
197 validateResolvFile(values);
198}
199
Ratan Guptac9645fe2017-08-23 16:53:52 +0530200TEST_F(TestEthernetInterface, addNTPServers)
201{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500202 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
Ratan Gupta35297172018-11-28 18:40:16 +0530203 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530204 interface.nTPServers(servers);
205 fs::path filePath = confDir;
206 filePath /= "00-bmc-test0.network";
207 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530208 config::ReturnCode rc = config::ReturnCode::SUCCESS;
209 config::ValueList values;
210 std::tie(rc, values) = parser.getValues("Network", "NTP");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500211 EXPECT_EQ(servers, values);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530212}
213
Gunnar Mills57d9c502018-09-14 14:42:34 -0500214} // namespace network
215} // namespace phosphor