blob: 30dee8a0e24ebddb2761f40dbdeb3ee9d006ffa7 [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 }
Ratan Gupta47722dc2017-05-26 18:32:23 +0530104};
105
106TEST_F(TestEthernetInterface, NoIPaddress)
107{
108 EXPECT_EQ(countIPObjects(), 0);
William A. Kennington IIIcb64b992019-04-21 18:45:07 -0700109 EXPECT_EQ(mac_address::toString(mac), interface.mACAddress());
Ratan Gupta47722dc2017-05-26 18:32:23 +0530110}
111
112TEST_F(TestEthernetInterface, AddIPAddress)
113{
114 IP::Protocol addressType = IP::Protocol::IPv4;
115 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
116 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530117}
118
119TEST_F(TestEthernetInterface, AddMultipleAddress)
120{
121 IP::Protocol addressType = IP::Protocol::IPv4;
122 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
123 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
124 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
125 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530126}
127
128TEST_F(TestEthernetInterface, DeleteIPAddress)
129{
130 IP::Protocol addressType = IP::Protocol::IPv4;
131 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
132 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
133 deleteIPObject("10.10.10.10");
134 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
135 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530136}
137
138TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
139{
140 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
141}
142
143TEST_F(TestEthernetInterface, CheckObjectPath)
144{
145 std::string ipaddress = "10.10.10.10";
146 uint8_t prefix = 16;
147 std::string gateway = "10.10.10.1";
148
149 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
150 std::stringstream hexId;
151
152 std::string hashString = ipaddress;
153 hashString += std::to_string(prefix);
154 hashString += gateway;
155
Gunnar Mills57d9c502018-09-14 14:42:34 -0500156 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530157 expectedObjectPath += hexId.str();
158
Gunnar Mills57d9c502018-09-14 14:42:34 -0500159 EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530160}
161
Ratan Gupta09f61572017-08-23 16:53:52 +0530162TEST_F(TestEthernetInterface, addNameServers)
163{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500164 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
Ratan Guptab4005972019-09-19 06:19:16 +0530165 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Ratan Gupta09f61572017-08-23 16:53:52 +0530166 interface.nameservers(servers);
167 fs::path filePath = confDir;
168 filePath /= "00-bmc-test0.network";
169 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530170 config::ReturnCode rc = config::ReturnCode::SUCCESS;
171 config::ValueList values;
172 std::tie(rc, values) = parser.getValues("Network", "DNS");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500173 EXPECT_EQ(servers, values);
Ratan Gupta09f61572017-08-23 16:53:52 +0530174}
175
Ratan Guptac9645fe2017-08-23 16:53:52 +0530176TEST_F(TestEthernetInterface, addNTPServers)
177{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500178 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
Ratan Gupta35297172018-11-28 18:40:16 +0530179 EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530180 interface.nTPServers(servers);
181 fs::path filePath = confDir;
182 filePath /= "00-bmc-test0.network";
183 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530184 config::ReturnCode rc = config::ReturnCode::SUCCESS;
185 config::ValueList values;
186 std::tie(rc, values) = parser.getValues("Network", "NTP");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500187 EXPECT_EQ(servers, values);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530188}
189
Gunnar Mills57d9c502018-09-14 14:42:34 -0500190} // namespace network
191} // namespace phosphor