xref: /openbmc/phosphor-networkd/test/test_ethernet_interface.cpp (revision 4b604171f5bbcfc97db33908f46cfe30cf30e95d)
1 #include "config_parser.hpp"
2 #include "ipaddress.hpp"
3 #include "mock_network_manager.hpp"
4 #include "mock_syscall.hpp"
5 #include "util.hpp"
6 
7 #include <arpa/inet.h>
8 #include <net/if.h>
9 #include <netinet/in.h>
10 #include <stdlib.h>
11 
12 #include <exception>
13 #include <fstream>
14 #include <sdbusplus/bus.hpp>
15 #include <stdplus/gtest/tmp.hpp>
16 
17 #include <gtest/gtest.h>
18 
19 namespace phosphor
20 {
21 namespace network
22 {
23 
24 class TestEthernetInterface : public stdplus::gtest::TestWithTmp
25 {
26   public:
27     sdbusplus::bus_t bus;
28     std::string confDir;
29     MockManager manager;
30     MockEthernetInterface interface;
31     TestEthernetInterface() :
32         bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
33         manager(bus, "/xyz/openbmc_test/network", confDir),
34         interface(makeInterface(bus, manager))
35 
36     {
37     }
38 
39     static constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
40 
41     static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus,
42                                                MockManager& manager)
43     {
44         mock_clear();
45         mock_addIF("test0", 1, mac);
46         return {bus, "/xyz/openbmc_test/network/test0",
47                 EthernetInterface::DHCPConf::none, manager, true};
48     }
49 
50     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())
59         {
60             return false;
61         }
62         return true;
63     }
64 
65     bool deleteIPObject(const std::string& ipaddress)
66     {
67         auto address = interface.getAddresses().find(ipaddress);
68         if (address == interface.getAddresses().end())
69         {
70             return false;
71         }
72         address->second->delete_();
73         return true;
74     }
75 
76     std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask,
77                               const std::string& gateway,
78                               IP::AddressOrigin origin)
79     {
80         IP::Protocol addressType = IP::Protocol::IPv4;
81 
82         return interface.generateObjectPath(addressType, ipaddress, subnetMask,
83                                             gateway, origin);
84     }
85 
86     void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
87                         uint8_t subnetMask, const std::string& gateway)
88     {
89         interface.ip(addressType, ipaddress, subnetMask, gateway);
90     }
91 };
92 
93 TEST_F(TestEthernetInterface, NoIPaddress)
94 {
95     EXPECT_EQ(countIPObjects(), 0);
96     EXPECT_EQ(mac_address::toString(mac), interface.macAddress());
97 }
98 
99 TEST_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"));
104 }
105 
106 TEST_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"));
113 }
114 
115 TEST_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"));
123 }
124 
125 TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
126 {
127     EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
128 }
129 
130 TEST_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";
135     IP::AddressOrigin origin = IP::AddressOrigin::Static;
136 
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;
143     hashString += convertForMessage(origin);
144 
145     hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
146     expectedObjectPath += hexId.str();
147 
148     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));
154 }
155 
156 TEST_F(TestEthernetInterface, addStaticNameServers)
157 {
158     ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
159     EXPECT_CALL(manager, reloadConfigs());
160     interface.staticNameServers(servers);
161     fs::path filePath = confDir;
162     filePath /= "00-bmc-test0.network";
163     config::Parser parser(filePath.string());
164     EXPECT_EQ(servers, parser.getValues("Network", "DNS"));
165 }
166 
167 TEST_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 
175 TEST_F(TestEthernetInterface, addNTPServers)
176 {
177     ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
178     EXPECT_CALL(manager, reloadConfigs());
179     interface.ntpServers(servers);
180     fs::path filePath = confDir;
181     filePath /= "00-bmc-test0.network";
182     config::Parser parser(filePath.string());
183     EXPECT_EQ(servers, parser.getValues("Network", "NTP"));
184 }
185 
186 TEST_F(TestEthernetInterface, addGateway)
187 {
188     std::string gateway = "10.3.3.3";
189     interface.defaultGateway(gateway);
190     EXPECT_EQ(interface.defaultGateway(), gateway);
191     interface.defaultGateway("");
192     EXPECT_EQ(interface.defaultGateway(), "");
193 }
194 
195 TEST_F(TestEthernetInterface, addGateway6)
196 {
197     std::string gateway6 = "ffff:ffff:ffff:fe80::1";
198     interface.defaultGateway6(gateway6);
199     EXPECT_EQ(interface.defaultGateway6(), gateway6);
200     interface.defaultGateway6("");
201     EXPECT_EQ(interface.defaultGateway6(), "");
202 }
203 
204 } // namespace network
205 } // namespace phosphor
206