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