blob: ac96190cd39c50c833762b6f345f25cc2ed2306e [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>
William A. Kennington III84bfe672022-07-13 14:15:30 -070015#include <stdplus/gtest/tmp.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050016
17#include <gtest/gtest.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +053018
19namespace phosphor
20{
21namespace network
22{
23
William A. Kennington III84bfe672022-07-13 14:15:30 -070024class TestEthernetInterface : public stdplus::gtest::TestWithTmp
Ratan Gupta47722dc2017-05-26 18:32:23 +053025{
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 public:
Patrick Williamsc38b0712022-07-22 19:26:54 -050027 sdbusplus::bus_t bus;
William A. Kennington III84bfe672022-07-13 14:15:30 -070028 std::string confDir;
Ratan Gupta35297172018-11-28 18:40:16 +053029 MockManager manager;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053030 MockEthernetInterface interface;
Gunnar Mills57d9c502018-09-14 14:42:34 -050031 TestEthernetInterface() :
William A. Kennington III84bfe672022-07-13 14:15:30 -070032 bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
33 manager(bus, "/xyz/openbmc_test/network", confDir),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070034 interface(makeInterface(bus, manager))
Ratan Gupta47722dc2017-05-26 18:32:23 +053035
Gunnar Mills57d9c502018-09-14 14:42:34 -050036 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053038
William A. Kennington IIIcb64b992019-04-21 18:45:07 -070039 static constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
40
Patrick Williamsc38b0712022-07-22 19:26:54 -050041 static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus,
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053042 MockManager& manager)
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070043 {
William A. Kennington III862275a2019-04-22 20:37:08 -070044 mock_clear();
William A. Kennington IIIcb64b992019-04-21 18:45:07 -070045 mock_addIF("test0", 1, mac);
William A. Kennington IIIa520a392022-08-08 12:17:34 -070046 return {bus,
47 "/xyz/openbmc_test/network/test0",
48 config::Parser(),
49 EthernetInterface::DHCPConf::none,
50 manager,
51 true};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070052 }
53
Gunnar Mills57d9c502018-09-14 14:42:34 -050054 int countIPObjects()
55 {
56 return interface.getAddresses().size();
57 }
58
59 bool isIPObjectExist(const std::string& ipaddress)
60 {
61 auto address = interface.getAddresses().find(ipaddress);
62 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053063 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050064 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053065 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050066 return true;
67 }
Ratan Gupta255d5142017-08-10 09:02:08 +053068
Gunnar Mills57d9c502018-09-14 14:42:34 -050069 bool deleteIPObject(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 address->second->delete_();
77 return true;
78 }
Ratan Gupta255d5142017-08-10 09:02:08 +053079
Gunnar Mills57d9c502018-09-14 14:42:34 -050080 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
Lei YU34027572021-08-11 15:23:52 +080081 const std::string& gateway,
82 IP::AddressOrigin origin)
Gunnar Mills57d9c502018-09-14 14:42:34 -050083 {
84 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053085
Gunnar Mills57d9c502018-09-14 14:42:34 -050086 return interface.generateObjectPath(addressType, ipaddress, subnetMask,
Lei YU34027572021-08-11 15:23:52 +080087 gateway, origin);
Gunnar Mills57d9c502018-09-14 14:42:34 -050088 }
89
90 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
91 uint8_t subnetMask, const std::string& gateway)
92 {
Patrick Williams6aef7692021-05-01 06:39:41 -050093 interface.ip(addressType, ipaddress, subnetMask, gateway);
Gunnar Mills57d9c502018-09-14 14:42:34 -050094 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053095};
96
97TEST_F(TestEthernetInterface, NoIPaddress)
98{
99 EXPECT_EQ(countIPObjects(), 0);
Patrick Williams6aef7692021-05-01 06:39:41 -0500100 EXPECT_EQ(mac_address::toString(mac), interface.macAddress());
Ratan Gupta47722dc2017-05-26 18:32:23 +0530101}
102
103TEST_F(TestEthernetInterface, AddIPAddress)
104{
105 IP::Protocol addressType = IP::Protocol::IPv4;
106 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
107 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530108}
109
110TEST_F(TestEthernetInterface, AddMultipleAddress)
111{
112 IP::Protocol addressType = IP::Protocol::IPv4;
113 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
114 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
115 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
116 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530117}
118
119TEST_F(TestEthernetInterface, DeleteIPAddress)
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 deleteIPObject("10.10.10.10");
125 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
126 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530127}
128
129TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
130{
131 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
132}
133
134TEST_F(TestEthernetInterface, CheckObjectPath)
135{
136 std::string ipaddress = "10.10.10.10";
137 uint8_t prefix = 16;
138 std::string gateway = "10.10.10.1";
Lei YU34027572021-08-11 15:23:52 +0800139 IP::AddressOrigin origin = IP::AddressOrigin::Static;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530140
141 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
142 std::stringstream hexId;
143
144 std::string hashString = ipaddress;
145 hashString += std::to_string(prefix);
146 hashString += gateway;
Lei YU34027572021-08-11 15:23:52 +0800147 hashString += convertForMessage(origin);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530148
Gunnar Mills57d9c502018-09-14 14:42:34 -0500149 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530150 expectedObjectPath += hexId.str();
151
Lei YU34027572021-08-11 15:23:52 +0800152 EXPECT_EQ(expectedObjectPath,
153 getObjectPath(ipaddress, prefix, gateway, origin));
154
155 origin = IP::AddressOrigin::DHCP;
156 EXPECT_NE(expectedObjectPath,
157 getObjectPath(ipaddress, prefix, gateway, origin));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530158}
159
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530160TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530161{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500162 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700163 EXPECT_CALL(manager, reloadConfigs());
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530164 interface.staticNameServers(servers);
Ratan Gupta09f61572017-08-23 16:53:52 +0530165 fs::path filePath = confDir;
166 filePath /= "00-bmc-test0.network";
167 config::Parser parser(filePath.string());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700168 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "DNS"));
Ratan Gupta09f61572017-08-23 16:53:52 +0530169}
170
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530171TEST_F(TestEthernetInterface, getDynamicNameServers)
172{
173 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
174 EXPECT_CALL(interface, getNameServerFromResolvd())
175 .WillRepeatedly(testing::Return(servers));
176 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
177}
178
Ratan Guptac9645fe2017-08-23 16:53:52 +0530179TEST_F(TestEthernetInterface, addNTPServers)
180{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500181 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700182 EXPECT_CALL(manager, reloadConfigs());
Patrick Williams6aef7692021-05-01 06:39:41 -0500183 interface.ntpServers(servers);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530184 fs::path filePath = confDir;
185 filePath /= "00-bmc-test0.network";
186 config::Parser parser(filePath.string());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700187 EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP"));
Ratan Guptac9645fe2017-08-23 16:53:52 +0530188}
189
Ravi Tejaa5a09442020-07-17 00:57:33 -0500190TEST_F(TestEthernetInterface, addGateway)
191{
192 std::string gateway = "10.3.3.3";
193 interface.defaultGateway(gateway);
194 EXPECT_EQ(interface.defaultGateway(), gateway);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800195 interface.defaultGateway("");
196 EXPECT_EQ(interface.defaultGateway(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500197}
198
199TEST_F(TestEthernetInterface, addGateway6)
200{
201 std::string gateway6 = "ffff:ffff:ffff:fe80::1";
202 interface.defaultGateway6(gateway6);
203 EXPECT_EQ(interface.defaultGateway6(), gateway6);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800204 interface.defaultGateway6("");
205 EXPECT_EQ(interface.defaultGateway6(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500206}
207
Gunnar Mills57d9c502018-09-14 14:42:34 -0500208} // namespace network
209} // namespace phosphor