blob: 96f00d15b66b524ad0b3f2a8176dab5dbdf90fa1 [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);
Johnathan Mantey817012a2020-01-30 15:07:39 -080046 return {bus, "/xyz/openbmc_test/network/test0",
47 EthernetInterface::DHCPConf::none, manager, true};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070048 }
49
Gunnar Mills57d9c502018-09-14 14:42:34 -050050 int countIPObjects()
51 {
52 return interface.getAddresses().size();
53 }
54
55 bool isIPObjectExist(const std::string& ipaddress)
56 {
57 auto address = interface.getAddresses().find(ipaddress);
58 if (address == interface.getAddresses().end())
Ratan Gupta255d5142017-08-10 09:02:08 +053059 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050060 return false;
Ratan Gupta255d5142017-08-10 09:02:08 +053061 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050062 return true;
63 }
Ratan Gupta255d5142017-08-10 09:02:08 +053064
Gunnar Mills57d9c502018-09-14 14:42:34 -050065 bool deleteIPObject(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 address->second->delete_();
73 return true;
74 }
Ratan Gupta255d5142017-08-10 09:02:08 +053075
Gunnar Mills57d9c502018-09-14 14:42:34 -050076 std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
Lei YU34027572021-08-11 15:23:52 +080077 const std::string& gateway,
78 IP::AddressOrigin origin)
Gunnar Mills57d9c502018-09-14 14:42:34 -050079 {
80 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta255d5142017-08-10 09:02:08 +053081
Gunnar Mills57d9c502018-09-14 14:42:34 -050082 return interface.generateObjectPath(addressType, ipaddress, subnetMask,
Lei YU34027572021-08-11 15:23:52 +080083 gateway, origin);
Gunnar Mills57d9c502018-09-14 14:42:34 -050084 }
85
86 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
87 uint8_t subnetMask, const std::string& gateway)
88 {
Patrick Williams6aef7692021-05-01 06:39:41 -050089 interface.ip(addressType, ipaddress, subnetMask, gateway);
Gunnar Mills57d9c502018-09-14 14:42:34 -050090 }
Ratan Gupta47722dc2017-05-26 18:32:23 +053091};
92
93TEST_F(TestEthernetInterface, NoIPaddress)
94{
95 EXPECT_EQ(countIPObjects(), 0);
Patrick Williams6aef7692021-05-01 06:39:41 -050096 EXPECT_EQ(mac_address::toString(mac), interface.macAddress());
Ratan Gupta47722dc2017-05-26 18:32:23 +053097}
98
99TEST_F(TestEthernetInterface, AddIPAddress)
100{
101 IP::Protocol addressType = IP::Protocol::IPv4;
102 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
103 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530104}
105
106TEST_F(TestEthernetInterface, AddMultipleAddress)
107{
108 IP::Protocol addressType = IP::Protocol::IPv4;
109 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
110 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
111 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
112 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530113}
114
115TEST_F(TestEthernetInterface, DeleteIPAddress)
116{
117 IP::Protocol addressType = IP::Protocol::IPv4;
118 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
119 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
120 deleteIPObject("10.10.10.10");
121 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
122 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530123}
124
125TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
126{
127 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
128}
129
130TEST_F(TestEthernetInterface, CheckObjectPath)
131{
132 std::string ipaddress = "10.10.10.10";
133 uint8_t prefix = 16;
134 std::string gateway = "10.10.10.1";
Lei YU34027572021-08-11 15:23:52 +0800135 IP::AddressOrigin origin = IP::AddressOrigin::Static;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530136
137 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
138 std::stringstream hexId;
139
140 std::string hashString = ipaddress;
141 hashString += std::to_string(prefix);
142 hashString += gateway;
Lei YU34027572021-08-11 15:23:52 +0800143 hashString += convertForMessage(origin);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530144
Gunnar Mills57d9c502018-09-14 14:42:34 -0500145 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta47722dc2017-05-26 18:32:23 +0530146 expectedObjectPath += hexId.str();
147
Lei YU34027572021-08-11 15:23:52 +0800148 EXPECT_EQ(expectedObjectPath,
149 getObjectPath(ipaddress, prefix, gateway, origin));
150
151 origin = IP::AddressOrigin::DHCP;
152 EXPECT_NE(expectedObjectPath,
153 getObjectPath(ipaddress, prefix, gateway, origin));
Ratan Gupta47722dc2017-05-26 18:32:23 +0530154}
155
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530156TEST_F(TestEthernetInterface, addStaticNameServers)
Ratan Gupta09f61572017-08-23 16:53:52 +0530157{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500158 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700159 EXPECT_CALL(manager, reloadConfigs());
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530160 interface.staticNameServers(servers);
Ratan Gupta09f61572017-08-23 16:53:52 +0530161 fs::path filePath = confDir;
162 filePath /= "00-bmc-test0.network";
163 config::Parser parser(filePath.string());
William A. Kennington III25511a12022-08-04 16:32:28 -0700164 EXPECT_EQ(servers, parser.getValues("Network", "DNS"));
Ratan Gupta09f61572017-08-23 16:53:52 +0530165}
166
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530167TEST_F(TestEthernetInterface, getDynamicNameServers)
168{
169 ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
170 EXPECT_CALL(interface, getNameServerFromResolvd())
171 .WillRepeatedly(testing::Return(servers));
172 EXPECT_EQ(interface.getNameServerFromResolvd(), servers);
173}
174
Ratan Guptac9645fe2017-08-23 16:53:52 +0530175TEST_F(TestEthernetInterface, addNTPServers)
176{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500177 ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700178 EXPECT_CALL(manager, reloadConfigs());
Patrick Williams6aef7692021-05-01 06:39:41 -0500179 interface.ntpServers(servers);
Ratan Guptac9645fe2017-08-23 16:53:52 +0530180 fs::path filePath = confDir;
181 filePath /= "00-bmc-test0.network";
182 config::Parser parser(filePath.string());
William A. Kennington III25511a12022-08-04 16:32:28 -0700183 EXPECT_EQ(servers, parser.getValues("Network", "NTP"));
Ratan Guptac9645fe2017-08-23 16:53:52 +0530184}
185
Ravi Tejaa5a09442020-07-17 00:57:33 -0500186TEST_F(TestEthernetInterface, addGateway)
187{
188 std::string gateway = "10.3.3.3";
189 interface.defaultGateway(gateway);
190 EXPECT_EQ(interface.defaultGateway(), gateway);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800191 interface.defaultGateway("");
192 EXPECT_EQ(interface.defaultGateway(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500193}
194
195TEST_F(TestEthernetInterface, addGateway6)
196{
197 std::string gateway6 = "ffff:ffff:ffff:fe80::1";
198 interface.defaultGateway6(gateway6);
199 EXPECT_EQ(interface.defaultGateway6(), gateway6);
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +0800200 interface.defaultGateway6("");
201 EXPECT_EQ(interface.defaultGateway6(), "");
Ravi Tejaa5a09442020-07-17 00:57:33 -0500202}
203
Gunnar Mills57d9c502018-09-14 14:42:34 -0500204} // namespace network
205} // namespace phosphor