blob: 9711a1aa709e74fbd396b2527db6cee966cd5e55 [file] [log] [blame]
Ratan Gupta2aca45c2017-07-26 01:16:27 +05301#include "network_manager.hpp"
2#include "mock_syscall.hpp"
3#include "config_parser.hpp"
4#include "vlan_interface.hpp"
5#include "ipaddress.hpp"
6
7#include <gtest/gtest.h>
8#include <sdbusplus/bus.hpp>
9
10#include <net/if.h>
11#include <netinet/in.h>
12#include <arpa/inet.h>
13#include <exception>
14#include <experimental/filesystem>
15
16namespace phosphor
17{
18namespace network
19{
20
21namespace fs = std::experimental::filesystem;
22
23class TestVlanInterface : public testing::Test
24{
25 public:
26
27 sdbusplus::bus::bus bus;
28 Manager manager;
29 EthernetInterface interface;
30 std::string confDir;
31 TestVlanInterface()
32 : bus(sdbusplus::bus::new_default()),
33 manager(bus, "/xyz/openbmc_test/network", "/tmp"),
34 interface(bus, "/xyz/openbmc_test/network/test0", false, manager)
35
36 {
37 setConfDir();
38 }
39
40 ~TestVlanInterface()
41 {
42 if(confDir != "")
43 {
44 fs::remove_all(confDir);
45 }
46 }
47
48 void setConfDir()
49 {
50 char tmp[] = "/tmp/VlanInterface.XXXXXX";
51 confDir = mkdtemp(tmp);
52 manager.setConfDir(confDir);
53 }
54
55 void createVlan(VlanId id)
56 {
57 interface.createVLAN(id);
58 }
59
60 void deleteVlan(const std::string& interfaceName)
61 {
62 interface.deleteVLANObject(interfaceName);
63 }
64
65 int countIPObjects()
66 {
67 return interface.getAddresses().size();
68 }
69
70 bool isIPObjectExist(const std::string& ipaddress)
71 {
72 auto address = interface.getAddresses().find(ipaddress);
73 if (address == interface.getAddresses().end())
74 {
75 return false;
76 }
77 return true;
78
79 }
80
81 bool deleteIPObject(const std::string& ipaddress)
82 {
83 auto address = interface.getAddresses().find(ipaddress);
84 if (address == interface.getAddresses().end())
85 {
86 return false;
87 }
88 address->second->delete_();
89 return true;
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 bool isValueFound(const std::vector<std::string>& values,
106 const std::string& expectedValue)
107 {
108 for (const auto& value : values)
109 {
110 if (expectedValue == value)
111 {
112 return true;
113 }
114 }
115 return false;
116 }
117};
118
119TEST_F(TestVlanInterface, createVLAN)
120{
121 createVlan(50);
122 fs::path filePath = confDir;
123 filePath /= "test0.50.netdev";
124
125 config::Parser parser(filePath.string());
126 auto values = parser.getValues("NetDev", "Name");
127 std::string expectedValue = "test0.50";
128 bool found = isValueFound(values, expectedValue);
129 EXPECT_EQ(found, true);
130
131 values = parser.getValues("NetDev", "Kind");
132 expectedValue = "vlan";
133 found = isValueFound(values, expectedValue);
134 EXPECT_EQ(found, true);
135
136 values = parser.getValues("VLAN", "Id");
137 expectedValue = "50";
138 found = isValueFound(values, expectedValue);
139 EXPECT_EQ(found, true);
140
141}
142
143TEST_F(TestVlanInterface, deleteVLAN)
144{
145 createVlan(50);
146 deleteVlan("test0.50");
147 bool fileFound = false;
148
149 fs::path filePath = confDir;
150 filePath /= "test0.50.netdev";
151 if (fs::is_regular_file(filePath.string()))
152 {
153 fileFound = true;
154 }
155 EXPECT_EQ(fileFound, false);
156}
157
158TEST_F(TestVlanInterface, createMultipleVLAN)
159{
160 createVlan(50);
161 createVlan(60);
162
163 fs::path filePath = confDir;
164 filePath /= "test0.50.netdev";
165 config::Parser parser(filePath.string());
166 auto values = parser.getValues("NetDev", "Name");
167 std::string expectedValue = "test0.50";
168 bool found = isValueFound(values, expectedValue);
169 EXPECT_EQ(found, true);
170
171 values = parser.getValues("NetDev", "Kind");
172 expectedValue = "vlan";
173 found = isValueFound(values, expectedValue);
174 EXPECT_EQ(found, true);
175
176 values = parser.getValues("VLAN", "Id");
177 expectedValue = "50";
178 found = isValueFound(values, expectedValue);
179 EXPECT_EQ(found, true);
180
181 filePath = confDir;
182 filePath /= "test0.60.netdev";
183 parser.setFile(filePath.string());
184 values = parser.getValues("NetDev", "Name");
185 expectedValue = "test0.60";
186 found = isValueFound(values, expectedValue);
187 EXPECT_EQ(found, true);
188
189 values = parser.getValues("VLAN", "Id");
190 expectedValue = "60";
191 found = isValueFound(values, expectedValue);
192 EXPECT_EQ(found, true);
193
194 deleteVlan("test0.50");
195 deleteVlan("test0.60");
196}
197
198}// namespce network
199}// namespace phosphor