blob: beaa0d4e3db37229277b7b631a0c1981e4ffa319 [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
7#include <net/if.h>
8#include <netinet/in.h>
9#include <arpa/inet.h>
10#include <exception>
11
12namespace phosphor
13{
14namespace network
15{
16
17class TestEthernetInterface : public testing::Test
18{
19 public:
20
21 sdbusplus::bus::bus bus;
22 Manager manager;
23 EthernetInterface interface;
24 TestEthernetInterface()
25 : bus(sdbusplus::bus::new_default()),
26 manager(bus, "/xyz/openbmc_test/network"),
27 interface(bus, "/xyz/openbmc_test/network/test0", false, manager)
28
29 {
30
31 }
32
33 int countIPObjects()
34 {
35 return interface.getAddresses().size();
36 }
37
38 bool isIPObjectExist(const std::string& ipaddress)
39 {
40 auto address = interface.getAddresses().find(ipaddress);
41 if (address == interface.getAddresses().end())
42 {
43 return false;
44 }
45 return true;
46
47 }
48
49 bool deleteIPObject(const std::string& ipaddress)
50 {
51 auto address = interface.getAddresses().find(ipaddress);
52 if (address == interface.getAddresses().end())
53 {
54 return false;
55 }
56 address->second->delete_();
57 return true;
58 }
59
60 std::string getObjectPath(const std::string& ipaddress,
61 uint8_t subnetMask,
62 const std::string& gateway)
63 {
64 IP::Protocol addressType = IP::Protocol::IPv4;
65
66 return interface.generateObjectPath(addressType,
67 ipaddress,
68 subnetMask,
69 gateway);
70 }
71
72 void createIPObject(IP::Protocol addressType,
73 const std::string& ipaddress,
74 uint8_t subnetMask,
75 const std::string& gateway)
76 {
77 interface.iP(addressType,
78 ipaddress,
79 subnetMask,
80 gateway
81 );
82
83 }
84
85};
86
87TEST_F(TestEthernetInterface, NoIPaddress)
88{
89 EXPECT_EQ(countIPObjects(), 0);
90
91}
92
93TEST_F(TestEthernetInterface, AddIPAddress)
94{
95 IP::Protocol addressType = IP::Protocol::IPv4;
96 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
97 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
98
99}
100
101TEST_F(TestEthernetInterface, AddMultipleAddress)
102{
103 IP::Protocol addressType = IP::Protocol::IPv4;
104 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
105 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
106 EXPECT_EQ(true, isIPObjectExist("10.10.10.10"));
107 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
108
109}
110
111TEST_F(TestEthernetInterface, DeleteIPAddress)
112{
113 IP::Protocol addressType = IP::Protocol::IPv4;
114 createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1");
115 createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1");
116 deleteIPObject("10.10.10.10");
117 EXPECT_EQ(false, isIPObjectExist("10.10.10.10"));
118 EXPECT_EQ(true, isIPObjectExist("20.20.20.20"));
119
120}
121
122TEST_F(TestEthernetInterface, DeleteInvalidIPAddress)
123{
124 EXPECT_EQ(false, deleteIPObject("10.10.10.10"));
125}
126
127TEST_F(TestEthernetInterface, CheckObjectPath)
128{
129 std::string ipaddress = "10.10.10.10";
130 uint8_t prefix = 16;
131 std::string gateway = "10.10.10.1";
132
133 std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/";
134 std::stringstream hexId;
135
136 std::string hashString = ipaddress;
137 hashString += std::to_string(prefix);
138 hashString += gateway;
139
140
141 hexId << std::hex << ((std::hash<std::string> {}(
142 hashString)) & 0xFFFFFFFF);
143 expectedObjectPath += hexId.str();
144
145 EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress,
146 prefix,
147 gateway));
148}
149
150}// namespce network
151}// namespace phosphor