blob: 21810062049a9effb414baf65716eb4de0e86978 [file] [log] [blame]
Ratan Guptac9645fe2017-08-23 16:53:52 +05301#include "config_parser.hpp"
Ratan Gupta47722dc2017-05-26 18:32:23 +05302#include "network_manager.hpp"
3#include "mock_syscall.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05304#include "ipaddress.hpp"
Ratan Gupta47722dc2017-05-26 18:32:23 +05305
6#include <gtest/gtest.h>
7#include <sdbusplus/bus.hpp>
8
Ratan Gupta255d5142017-08-10 09:02:08 +05309#include <arpa/inet.h>
Ratan Gupta47722dc2017-05-26 18:32:23 +053010#include <net/if.h>
11#include <netinet/in.h>
Ratan Gupta255d5142017-08-10 09:02:08 +053012#include <stdlib.h>
13
Ratan Gupta47722dc2017-05-26 18:32:23 +053014#include <exception>
Ratan Guptac9645fe2017-08-23 16:53:52 +053015#include <fstream>
Ratan Gupta47722dc2017-05-26 18:32:23 +053016
17namespace phosphor
18{
19namespace network
20{
21
22class TestEthernetInterface : public testing::Test
23{
24 public:
25
26 sdbusplus::bus::bus bus;
27 Manager manager;
28 EthernetInterface interface;
Ratan Gupta255d5142017-08-10 09:02:08 +053029 std::string confDir;
Ratan Gupta47722dc2017-05-26 18:32:23 +053030 TestEthernetInterface()
31 : bus(sdbusplus::bus::new_default()),
Ratan Gupta255d5142017-08-10 09:02:08 +053032 manager(bus, "/xyz/openbmc_test/network", "/tmp/"),
Ratan Gupta47722dc2017-05-26 18:32:23 +053033 interface(bus, "/xyz/openbmc_test/network/test0", false, manager)
34
35 {
Ratan Gupta255d5142017-08-10 09:02:08 +053036 setConfDir();
Ratan Gupta47722dc2017-05-26 18:32:23 +053037 }
38
Ratan Gupta255d5142017-08-10 09:02:08 +053039 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 != "")
49 {
50 fs::remove_all(confDir);
51 }
52 }
53
54
Ratan Gupta47722dc2017-05-26 18:32:23 +053055 int countIPObjects()
56 {
57 return interface.getAddresses().size();
58 }
59
60 bool isIPObjectExist(const std::string& ipaddress)
61 {
62 auto address = interface.getAddresses().find(ipaddress);
63 if (address == interface.getAddresses().end())
64 {
65 return false;
66 }
67 return true;
68
69 }
70
71 bool deleteIPObject(const std::string& ipaddress)
72 {
73 auto address = interface.getAddresses().find(ipaddress);
74 if (address == interface.getAddresses().end())
75 {
76 return false;
77 }
78 address->second->delete_();
79 return true;
80 }
81
82 std::string getObjectPath(const std::string& ipaddress,
83 uint8_t subnetMask,
84 const std::string& gateway)
85 {
86 IP::Protocol addressType = IP::Protocol::IPv4;
87
88 return interface.generateObjectPath(addressType,
89 ipaddress,
90 subnetMask,
91 gateway);
92 }
93
94 void createIPObject(IP::Protocol addressType,
95 const std::string& ipaddress,
96 uint8_t subnetMask,
97 const std::string& gateway)
98 {
99 interface.iP(addressType,
100 ipaddress,
101 subnetMask,
102 gateway
103 );
104
105 }
106
Ratan Gupta09f61572017-08-23 16:53:52 +0530107 // Validates if the DNS entries have been correctly processed
108 void validateResolvFile(ServerList values)
109 {
110 // Check whether the entries has been written to resolv.conf
111 fs::path resolvFile = confDir;
112 resolvFile /= "resolv.conf";
113
114 // Passed in "value" is what is read from the config file
115 interface.writeDNSEntries(values, resolvFile);
116 std::string expectedServers = "### Generated manually via dbus settings ###";
117 expectedServers +=
118 "nameserver 9.1.1.1nameserver 9.2.2.2nameserver 9.3.3.3";
119
120 std::string actualServers{};
121 std::fstream stream(resolvFile.string().c_str(), std::fstream::in);
122 for (std::string line; std::getline(stream, line);)
123 {
124 actualServers += line;
125 }
126 EXPECT_EQ(expectedServers , actualServers);
127 }
Ratan Gupta47722dc2017-05-26 18:32:23 +0530128};
129
130TEST_F(TestEthernetInterface, NoIPaddress)
131{
132 EXPECT_EQ(countIPObjects(), 0);
133
134}
135
136TEST_F(TestEthernetInterface, AddIPAddress)
137{
138 IP::Protocol addressType = IP::Protocol::IPv4;
139 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
140 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
141
142}
143
144TEST_F(TestEthernetInterface, AddMultipleAddress)
145{
146 IP::Protocol addressType = IP::Protocol::IPv4;
147 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
148 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
149 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
150 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
151
152}
153
154TEST_F(TestEthernetInterface, DeleteIPAddress)
155{
156 IP::Protocol addressType = IP::Protocol::IPv4;
157 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
158 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
159 deleteIPObject("10.10.10.10");
160 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
161 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
162
163}
164
165TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
166{
167 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
168}
169
170TEST_F(TestEthernetInterface, CheckObjectPath)
171{
172 std::string ipaddress = "10.10.10.10";
173 uint8_t prefix = 16;
174 std::string gateway = "10.10.10.1";
175
176 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
177 std::stringstream hexId;
178
179 std::string hashString = ipaddress;
180 hashString += std::to_string(prefix);
181 hashString += gateway;
182
183
184 hexId << std::hex << ((std::hash<std::string> {}(
185 hashString)) & 0xFFFFFFFF);
186 expectedObjectPath += hexId.str();
187
188 EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress,
189 prefix,
190 gateway));
191}
192
Ratan Gupta09f61572017-08-23 16:53:52 +0530193TEST_F(TestEthernetInterface, addNameServers)
194{
195 ServerList servers = {"9.1.1.1","9.2.2.2","9.3.3.3"};
196 interface.nameservers(servers);
197 fs::path filePath = confDir;
198 filePath /= "00-bmc-test0.network";
199 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530200 config::ReturnCode rc = config::ReturnCode::SUCCESS;
201 config::ValueList values;
202 std::tie(rc, values) = parser.getValues("Network", "DNS");
Ratan Gupta09f61572017-08-23 16:53:52 +0530203 EXPECT_EQ(servers , values);
204
205 validateResolvFile(values);
206}
207
Ratan Guptac9645fe2017-08-23 16:53:52 +0530208TEST_F(TestEthernetInterface, addNTPServers)
209{
210 ServerList servers = {"10.1.1.1","10.2.2.2","10.3.3.3"};
211 interface.nTPServers(servers);
212 fs::path filePath = confDir;
213 filePath /= "00-bmc-test0.network";
214 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530215 config::ReturnCode rc = config::ReturnCode::SUCCESS;
216 config::ValueList values;
217 std::tie(rc, values) = parser.getValues("Network", "NTP");
Ratan Guptac9645fe2017-08-23 16:53:52 +0530218 EXPECT_EQ(servers , values);
219}
220
Ratan Gupta47722dc2017-05-26 18:32:23 +0530221}// namespce network
222}// namespace phosphor