blob: edc12fbe3e28b1cfd07732e271cce617c4d6781d [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
107};
108
109TEST_F(TestEthernetInterface, NoIPaddress)
110{
111 EXPECT_EQ(countIPObjects(), 0);
112
113}
114
115TEST_F(TestEthernetInterface, AddIPAddress)
116{
117 IP::Protocol addressType = IP::Protocol::IPv4;
118 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
119 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
120
121}
122
123TEST_F(TestEthernetInterface, AddMultipleAddress)
124{
125 IP::Protocol addressType = IP::Protocol::IPv4;
126 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
127 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
128 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
129 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
130
131}
132
133TEST_F(TestEthernetInterface, DeleteIPAddress)
134{
135 IP::Protocol addressType = IP::Protocol::IPv4;
136 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
137 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
138 deleteIPObject("10.10.10.10");
139 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
140 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
141
142}
143
144TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
145{
146 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
147}
148
149TEST_F(TestEthernetInterface, CheckObjectPath)
150{
151 std::string ipaddress = "10.10.10.10";
152 uint8_t prefix = 16;
153 std::string gateway = "10.10.10.1";
154
155 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
156 std::stringstream hexId;
157
158 std::string hashString = ipaddress;
159 hashString += std::to_string(prefix);
160 hashString += gateway;
161
162
163 hexId << std::hex << ((std::hash<std::string> {}(
164 hashString)) & 0xFFFFFFFF);
165 expectedObjectPath += hexId.str();
166
167 EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress,
168 prefix,
169 gateway));
170}
171
Ratan Guptac9645fe2017-08-23 16:53:52 +0530172TEST_F(TestEthernetInterface, addNTPServers)
173{
174 ServerList servers = {"10.1.1.1","10.2.2.2","10.3.3.3"};
175 interface.nTPServers(servers);
176 fs::path filePath = confDir;
177 filePath /= "00-bmc-test0.network";
178 config::Parser parser(filePath.string());
179 auto values = parser.getValues("Network", "NTP");
180 EXPECT_EQ(servers , values);
181}
182
Ratan Gupta47722dc2017-05-26 18:32:23 +0530183}// namespce network
184}// namespace phosphor