blob: 67a25dfc89c945283fdf1cbfe84e665609740fba [file] [log] [blame]
Ratan Gupta47722dc2017-05-26 18:32:23 +05301#include "network_manager.hpp"
2#include "mock_syscall.hpp"
3
4#include <gtest/gtest.h>
5#include <sdbusplus/bus.hpp>
6
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>
13
14namespace phosphor
15{
16namespace network
17{
18
19class TestEthernetInterface : public testing::Test
20{
21 public:
22
23 sdbusplus::bus::bus bus;
24 Manager manager;
25 EthernetInterface interface;
Ratan Gupta255d5142017-08-10 09:02:08 +053026 std::string confDir;
Ratan Gupta47722dc2017-05-26 18:32:23 +053027 TestEthernetInterface()
28 : bus(sdbusplus::bus::new_default()),
Ratan Gupta255d5142017-08-10 09:02:08 +053029 manager(bus, "/xyz/openbmc_test/network", "/tmp/"),
Ratan Gupta47722dc2017-05-26 18:32:23 +053030 interface(bus, "/xyz/openbmc_test/network/test0", false, manager)
31
32 {
Ratan Gupta255d5142017-08-10 09:02:08 +053033 setConfDir();
Ratan Gupta47722dc2017-05-26 18:32:23 +053034
35 }
36
Ratan Gupta255d5142017-08-10 09:02:08 +053037 void setConfDir()
38 {
39 char tmp[] = "/tmp/EthernetInterface.XXXXXX";
40 confDir = mkdtemp(tmp);
41 manager.setConfDir(confDir);
42 }
43
44 ~TestEthernetInterface()
45 {
46 if(confDir != "")
47 {
48 fs::remove_all(confDir);
49 }
50 }
51
52
Ratan Gupta47722dc2017-05-26 18:32:23 +053053 int countIPObjects()
54 {
55 return interface.getAddresses().size();
56 }
57
58 bool isIPObjectExist(const std::string& ipaddress)
59 {
60 auto address = interface.getAddresses().find(ipaddress);
61 if (address == interface.getAddresses().end())
62 {
63 return false;
64 }
65 return true;
66
67 }
68
69 bool deleteIPObject(const std::string& ipaddress)
70 {
71 auto address = interface.getAddresses().find(ipaddress);
72 if (address == interface.getAddresses().end())
73 {
74 return false;
75 }
76 address->second->delete_();
77 return true;
78 }
79
80 std::string getObjectPath(const std::string& ipaddress,
81 uint8_t subnetMask,
82 const std::string& gateway)
83 {
84 IP::Protocol addressType = IP::Protocol::IPv4;
85
86 return interface.generateObjectPath(addressType,
87 ipaddress,
88 subnetMask,
89 gateway);
90 }
91
92 void createIPObject(IP::Protocol addressType,
93 const std::string& ipaddress,
94 uint8_t subnetMask,
95 const std::string& gateway)
96 {
97 interface.iP(addressType,
98 ipaddress,
99 subnetMask,
100 gateway
101 );
102
103 }
104
105};
106
107TEST_F(TestEthernetInterface, NoIPaddress)
108{
109 EXPECT_EQ(countIPObjects(), 0);
110
111}
112
113TEST_F(TestEthernetInterface, AddIPAddress)
114{
115 IP::Protocol addressType = IP::Protocol::IPv4;
116 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
117 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
118
119}
120
121TEST_F(TestEthernetInterface, AddMultipleAddress)
122{
123 IP::Protocol addressType = IP::Protocol::IPv4;
124 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
125 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
126 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
127 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
128
129}
130
131TEST_F(TestEthernetInterface, DeleteIPAddress)
132{
133 IP::Protocol addressType = IP::Protocol::IPv4;
134 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
135 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
136 deleteIPObject("10.10.10.10");
137 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
138 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
139
140}
141
142TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
143{
144 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
145}
146
147TEST_F(TestEthernetInterface, CheckObjectPath)
148{
149 std::string ipaddress = "10.10.10.10";
150 uint8_t prefix = 16;
151 std::string gateway = "10.10.10.1";
152
153 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
154 std::stringstream hexId;
155
156 std::string hashString = ipaddress;
157 hashString += std::to_string(prefix);
158 hashString += gateway;
159
160
161 hexId << std::hex << ((std::hash<std::string> {}(
162 hashString)) & 0xFFFFFFFF);
163 expectedObjectPath += hexId.str();
164
165 EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress,
166 prefix,
167 gateway));
168}
169
170}// namespce network
171}// namespace phosphor