blob: 0ff893c428855841324efa0fc22860a112293fd3 [file] [log] [blame]
Ratan Gupta2aca45c2017-07-26 01:16:27 +05301#include "config_parser.hpp"
Ratan Gupta2aca45c2017-07-26 01:16:27 +05302#include "ipaddress.hpp"
Ratan Gupta35297172018-11-28 18:40:16 +05303#include "mock_network_manager.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05004#include "mock_syscall.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05005#include "vlan_interface.hpp"
Ratan Gupta2aca45c2017-07-26 01:16:27 +05306
Gunnar Mills57d9c502018-09-14 14:42:34 -05007#include <arpa/inet.h>
Ratan Gupta2aca45c2017-07-26 01:16:27 +05308#include <net/if.h>
9#include <netinet/in.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -050010
Ratan Gupta2aca45c2017-07-26 01:16:27 +053011#include <exception>
12#include <experimental/filesystem>
Gunnar Mills57d9c502018-09-14 14:42:34 -050013#include <sdbusplus/bus.hpp>
14
15#include <gtest/gtest.h>
Ratan Gupta2aca45c2017-07-26 01:16:27 +053016
17namespace phosphor
18{
19namespace network
20{
21
22namespace fs = std::experimental::filesystem;
23
24class TestVlanInterface : public testing::Test
25{
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 public:
27 sdbusplus::bus::bus bus;
Ratan Gupta35297172018-11-28 18:40:16 +053028 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050029 EthernetInterface interface;
30 std::string confDir;
31 TestVlanInterface() :
32 bus(sdbusplus::bus::new_default()),
33 manager(bus, "/xyz/openbmc_test/network", "/tmp"),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070034 interface(makeInterface(bus, manager))
Ratan Gupta2aca45c2017-07-26 01:16:27 +053035
Gunnar Mills57d9c502018-09-14 14:42:34 -050036 {
37 setConfDir();
38 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +053039
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 ~TestVlanInterface()
41 {
42 if (confDir != "")
Ratan Gupta2aca45c2017-07-26 01:16:27 +053043 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 fs::remove_all(confDir);
Ratan Gupta2aca45c2017-07-26 01:16:27 +053045 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +053047
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070048 static EthernetInterface makeInterface(sdbusplus::bus::bus& bus,
49 MockManager& manager)
50 {
51 mock_addIF("test0", 1);
52 return {bus, "/xyz/openbmc_test/network/test0", false, manager};
53 }
54
Gunnar Mills57d9c502018-09-14 14:42:34 -050055 void setConfDir()
56 {
57 char tmp[] = "/tmp/VlanInterface.XXXXXX";
58 confDir = mkdtemp(tmp);
59 manager.setConfDir(confDir);
60 }
61
62 void createVlan(VlanId id)
63 {
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070064 std::string ifname = "test0.";
65 ifname += std::to_string(id);
66 mock_addIF(ifname.c_str(), 1000 + id);
Gunnar Mills57d9c502018-09-14 14:42:34 -050067 interface.createVLAN(id);
68 }
69
70 void deleteVlan(const std::string& interfaceName)
71 {
72 interface.deleteVLANObject(interfaceName);
73 }
74
75 int countIPObjects()
76 {
77 return interface.getAddresses().size();
78 }
79
80 bool isIPObjectExist(const std::string& ipaddress)
81 {
82 auto address = interface.getAddresses().find(ipaddress);
83 if (address == interface.getAddresses().end())
Ratan Gupta2aca45c2017-07-26 01:16:27 +053084 {
Ratan Gupta2aca45c2017-07-26 01:16:27 +053085 return false;
86 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050087 return true;
88 }
89
90 bool deleteIPObject(const std::string& ipaddress)
91 {
92 auto address = interface.getAddresses().find(ipaddress);
93 if (address == interface.getAddresses().end())
94 {
95 return false;
96 }
97 address->second->delete_();
98 return true;
99 }
100
101 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
102 uint8_t subnetMask, const std::string& gateway)
103 {
104 interface.iP(addressType, ipaddress, subnetMask, gateway);
105 }
106
107 bool isValueFound(const std::vector<std::string>& values,
108 const std::string& expectedValue)
109 {
110 for (const auto& value : values)
111 {
112 if (expectedValue == value)
113 {
114 return true;
115 }
116 }
117 return false;
118 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530119};
120
121TEST_F(TestVlanInterface, createVLAN)
122{
123 createVlan(50);
124 fs::path filePath = confDir;
125 filePath /= "test0.50.netdev";
126
127 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530128 config::ReturnCode rc = config::ReturnCode::SUCCESS;
129 config::ValueList values;
130
131 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530132 std::string expectedValue = "test0.50";
133 bool found = isValueFound(values, expectedValue);
134 EXPECT_EQ(found, true);
135
Ratan Guptac27170a2017-11-22 15:44:42 +0530136 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530137 expectedValue = "vlan";
138 found = isValueFound(values, expectedValue);
139 EXPECT_EQ(found, true);
140
Ratan Guptac27170a2017-11-22 15:44:42 +0530141 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530142 expectedValue = "50";
143 found = isValueFound(values, expectedValue);
144 EXPECT_EQ(found, true);
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530145}
146
147TEST_F(TestVlanInterface, deleteVLAN)
148{
149 createVlan(50);
150 deleteVlan("test0.50");
151 bool fileFound = false;
152
153 fs::path filePath = confDir;
154 filePath /= "test0.50.netdev";
155 if (fs::is_regular_file(filePath.string()))
156 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500157 fileFound = true;
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530158 }
159 EXPECT_EQ(fileFound, false);
160}
161
162TEST_F(TestVlanInterface, createMultipleVLAN)
163{
164 createVlan(50);
165 createVlan(60);
166
167 fs::path filePath = confDir;
168 filePath /= "test0.50.netdev";
169 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530170 config::ReturnCode rc = config::ReturnCode::SUCCESS;
171 config::ValueList values;
172
173 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530174 std::string expectedValue = "test0.50";
175 bool found = isValueFound(values, expectedValue);
176 EXPECT_EQ(found, true);
177
Ratan Guptac27170a2017-11-22 15:44:42 +0530178 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530179 expectedValue = "vlan";
180 found = isValueFound(values, expectedValue);
181 EXPECT_EQ(found, true);
182
Ratan Guptac27170a2017-11-22 15:44:42 +0530183 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530184 expectedValue = "50";
185 found = isValueFound(values, expectedValue);
186 EXPECT_EQ(found, true);
187
188 filePath = confDir;
189 filePath /= "test0.60.netdev";
190 parser.setFile(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530191 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530192 expectedValue = "test0.60";
193 found = isValueFound(values, expectedValue);
194 EXPECT_EQ(found, true);
195
Ratan Guptac27170a2017-11-22 15:44:42 +0530196 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530197 expectedValue = "60";
198 found = isValueFound(values, expectedValue);
199 EXPECT_EQ(found, true);
200
201 deleteVlan("test0.50");
202 deleteVlan("test0.60");
203}
204
Gunnar Mills57d9c502018-09-14 14:42:34 -0500205} // namespace network
206} // namespace phosphor