blob: 76c0828c7bc5bf49a2726a42d80433e453bd5f87 [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>
Manojkiran Edaa879baa2020-06-13 14:39:08 +053012#include <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
Manojkiran Edaa879baa2020-06-13 14:39:08 +053022namespace fs = std::filesystem;
Ratan Gupta2aca45c2017-07-26 01:16:27 +053023
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 {
William A. Kennington III862275a2019-04-22 20:37:08 -070051 mock_clear();
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070052 mock_addIF("test0", 1);
53 return {bus, "/xyz/openbmc_test/network/test0", false, manager};
54 }
55
Gunnar Mills57d9c502018-09-14 14:42:34 -050056 void setConfDir()
57 {
58 char tmp[] = "/tmp/VlanInterface.XXXXXX";
59 confDir = mkdtemp(tmp);
60 manager.setConfDir(confDir);
61 }
62
63 void createVlan(VlanId id)
64 {
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070065 std::string ifname = "test0.";
66 ifname += std::to_string(id);
67 mock_addIF(ifname.c_str(), 1000 + id);
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 interface.createVLAN(id);
69 }
70
71 void deleteVlan(const std::string& interfaceName)
72 {
73 interface.deleteVLANObject(interfaceName);
74 }
75
76 int countIPObjects()
77 {
78 return interface.getAddresses().size();
79 }
80
81 bool isIPObjectExist(const std::string& ipaddress)
82 {
83 auto address = interface.getAddresses().find(ipaddress);
84 if (address == interface.getAddresses().end())
Ratan Gupta2aca45c2017-07-26 01:16:27 +053085 {
Ratan Gupta2aca45c2017-07-26 01:16:27 +053086 return false;
87 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050088 return true;
89 }
90
91 bool deleteIPObject(const std::string& ipaddress)
92 {
93 auto address = interface.getAddresses().find(ipaddress);
94 if (address == interface.getAddresses().end())
95 {
96 return false;
97 }
98 address->second->delete_();
99 return true;
100 }
101
102 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
103 uint8_t subnetMask, const std::string& gateway)
104 {
105 interface.iP(addressType, ipaddress, subnetMask, gateway);
106 }
107
108 bool isValueFound(const std::vector<std::string>& values,
109 const std::string& expectedValue)
110 {
111 for (const auto& value : values)
112 {
113 if (expectedValue == value)
114 {
115 return true;
116 }
117 }
118 return false;
119 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530120};
121
122TEST_F(TestVlanInterface, createVLAN)
123{
124 createVlan(50);
125 fs::path filePath = confDir;
126 filePath /= "test0.50.netdev";
127
128 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530129 config::ReturnCode rc = config::ReturnCode::SUCCESS;
130 config::ValueList values;
131
132 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530133 std::string expectedValue = "test0.50";
134 bool found = isValueFound(values, expectedValue);
135 EXPECT_EQ(found, true);
136
Ratan Guptac27170a2017-11-22 15:44:42 +0530137 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530138 expectedValue = "vlan";
139 found = isValueFound(values, expectedValue);
140 EXPECT_EQ(found, true);
141
Ratan Guptac27170a2017-11-22 15:44:42 +0530142 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530143 expectedValue = "50";
144 found = isValueFound(values, expectedValue);
145 EXPECT_EQ(found, true);
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530146}
147
148TEST_F(TestVlanInterface, deleteVLAN)
149{
150 createVlan(50);
151 deleteVlan("test0.50");
152 bool fileFound = false;
153
154 fs::path filePath = confDir;
155 filePath /= "test0.50.netdev";
156 if (fs::is_regular_file(filePath.string()))
157 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500158 fileFound = true;
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530159 }
160 EXPECT_EQ(fileFound, false);
161}
162
163TEST_F(TestVlanInterface, createMultipleVLAN)
164{
165 createVlan(50);
166 createVlan(60);
167
168 fs::path filePath = confDir;
169 filePath /= "test0.50.netdev";
170 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530171 config::ReturnCode rc = config::ReturnCode::SUCCESS;
172 config::ValueList values;
173
174 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530175 std::string expectedValue = "test0.50";
176 bool found = isValueFound(values, expectedValue);
177 EXPECT_EQ(found, true);
178
Ratan Guptac27170a2017-11-22 15:44:42 +0530179 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530180 expectedValue = "vlan";
181 found = isValueFound(values, expectedValue);
182 EXPECT_EQ(found, true);
183
Ratan Guptac27170a2017-11-22 15:44:42 +0530184 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530185 expectedValue = "50";
186 found = isValueFound(values, expectedValue);
187 EXPECT_EQ(found, true);
188
189 filePath = confDir;
190 filePath /= "test0.60.netdev";
191 parser.setFile(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530192 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530193 expectedValue = "test0.60";
194 found = isValueFound(values, expectedValue);
195 EXPECT_EQ(found, true);
196
Ratan Guptac27170a2017-11-22 15:44:42 +0530197 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530198 expectedValue = "60";
199 found = isValueFound(values, expectedValue);
200 EXPECT_EQ(found, true);
201
202 deleteVlan("test0.50");
203 deleteVlan("test0.60");
204}
205
Gunnar Mills57d9c502018-09-14 14:42:34 -0500206} // namespace network
207} // namespace phosphor