blob: 00384f1fcc35e312ec39e4c10875890e610554ae [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());
Ratan Guptac27170a2017-11-22 15:44:42 +0530126 config::ReturnCode rc = config::ReturnCode::SUCCESS;
127 config::ValueList values;
128
129 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530130 std::string expectedValue = "test0.50";
131 bool found = isValueFound(values, expectedValue);
132 EXPECT_EQ(found, true);
133
Ratan Guptac27170a2017-11-22 15:44:42 +0530134 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530135 expectedValue = "vlan";
136 found = isValueFound(values, expectedValue);
137 EXPECT_EQ(found, true);
138
Ratan Guptac27170a2017-11-22 15:44:42 +0530139 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530140 expectedValue = "50";
141 found = isValueFound(values, expectedValue);
142 EXPECT_EQ(found, true);
143
144}
145
146TEST_F(TestVlanInterface, deleteVLAN)
147{
148 createVlan(50);
149 deleteVlan("test0.50");
150 bool fileFound = false;
151
152 fs::path filePath = confDir;
153 filePath /= "test0.50.netdev";
154 if (fs::is_regular_file(filePath.string()))
155 {
156 fileFound = true;
157 }
158 EXPECT_EQ(fileFound, false);
159}
160
161TEST_F(TestVlanInterface, createMultipleVLAN)
162{
163 createVlan(50);
164 createVlan(60);
165
166 fs::path filePath = confDir;
167 filePath /= "test0.50.netdev";
168 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530169 config::ReturnCode rc = config::ReturnCode::SUCCESS;
170 config::ValueList values;
171
172 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530173 std::string expectedValue = "test0.50";
174 bool found = isValueFound(values, expectedValue);
175 EXPECT_EQ(found, true);
176
Ratan Guptac27170a2017-11-22 15:44:42 +0530177 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530178 expectedValue = "vlan";
179 found = isValueFound(values, expectedValue);
180 EXPECT_EQ(found, true);
181
Ratan Guptac27170a2017-11-22 15:44:42 +0530182 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530183 expectedValue = "50";
184 found = isValueFound(values, expectedValue);
185 EXPECT_EQ(found, true);
186
187 filePath = confDir;
188 filePath /= "test0.60.netdev";
189 parser.setFile(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530190 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530191 expectedValue = "test0.60";
192 found = isValueFound(values, expectedValue);
193 EXPECT_EQ(found, true);
194
Ratan Guptac27170a2017-11-22 15:44:42 +0530195 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530196 expectedValue = "60";
197 found = isValueFound(values, expectedValue);
198 EXPECT_EQ(found, true);
199
200 deleteVlan("test0.50");
201 deleteVlan("test0.60");
202}
203
204}// namespce network
205}// namespace phosphor