blob: ea8eb01bdfeb2dd8d5dd86e73eb3c11348ba5f1b [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>
William A. Kennington III84bfe672022-07-13 14:15:30 -070014#include <stdplus/gtest/tmp.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050015
16#include <gtest/gtest.h>
Ratan Gupta2aca45c2017-07-26 01:16:27 +053017
18namespace phosphor
19{
20namespace network
21{
22
Manojkiran Edaa879baa2020-06-13 14:39:08 +053023namespace fs = std::filesystem;
Ratan Gupta2aca45c2017-07-26 01:16:27 +053024
William A. Kennington III84bfe672022-07-13 14:15:30 -070025class TestVlanInterface : public stdplus::gtest::TestWithTmp
Ratan Gupta2aca45c2017-07-26 01:16:27 +053026{
Gunnar Mills57d9c502018-09-14 14:42:34 -050027 public:
Patrick Williamsc38b0712022-07-22 19:26:54 -050028 sdbusplus::bus_t bus;
William A. Kennington III84bfe672022-07-13 14:15:30 -070029 std::string confDir;
Ratan Gupta35297172018-11-28 18:40:16 +053030 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050031 EthernetInterface interface;
Gunnar Mills57d9c502018-09-14 14:42:34 -050032 TestVlanInterface() :
William A. Kennington III84bfe672022-07-13 14:15:30 -070033 bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
34 manager(bus, "/xyz/openbmc_test/network", confDir),
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070035 interface(makeInterface(bus, manager))
Ratan Gupta2aca45c2017-07-26 01:16:27 +053036
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050038 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +053039
Patrick Williamsc38b0712022-07-22 19:26:54 -050040 static EthernetInterface makeInterface(sdbusplus::bus_t& bus,
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070041 MockManager& manager)
42 {
William A. Kennington III862275a2019-04-22 20:37:08 -070043 mock_clear();
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070044 mock_addIF("test0", 1);
William A. Kennington III26275a32021-07-13 20:32:42 -070045 return {bus,
46 "/xyz/openbmc_test/network/test0",
47 EthernetInterface::DHCPConf::none,
48 manager,
49 false,
50 true};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070051 }
52
Gunnar Mills57d9c502018-09-14 14:42:34 -050053 void createVlan(VlanId id)
54 {
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070055 std::string ifname = "test0.";
56 ifname += std::to_string(id);
57 mock_addIF(ifname.c_str(), 1000 + id);
Gunnar Mills57d9c502018-09-14 14:42:34 -050058 interface.createVLAN(id);
59 }
60
61 void deleteVlan(const std::string& interfaceName)
62 {
63 interface.deleteVLANObject(interfaceName);
64 }
65
66 int countIPObjects()
67 {
68 return interface.getAddresses().size();
69 }
70
71 bool isIPObjectExist(const std::string& ipaddress)
72 {
73 auto address = interface.getAddresses().find(ipaddress);
74 if (address == interface.getAddresses().end())
Ratan Gupta2aca45c2017-07-26 01:16:27 +053075 {
Ratan Gupta2aca45c2017-07-26 01:16:27 +053076 return false;
77 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050078 return true;
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, const std::string& ipaddress,
93 uint8_t subnetMask, const std::string& gateway)
94 {
Patrick Williams6aef7692021-05-01 06:39:41 -050095 interface.ip(addressType, ipaddress, subnetMask, gateway);
Gunnar Mills57d9c502018-09-14 14:42:34 -050096 }
97
98 bool isValueFound(const std::vector<std::string>& values,
99 const std::string& expectedValue)
100 {
101 for (const auto& value : values)
102 {
103 if (expectedValue == value)
104 {
105 return true;
106 }
107 }
108 return false;
109 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530110};
111
112TEST_F(TestVlanInterface, createVLAN)
113{
114 createVlan(50);
115 fs::path filePath = confDir;
116 filePath /= "test0.50.netdev";
117
118 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530119 config::ReturnCode rc = config::ReturnCode::SUCCESS;
120 config::ValueList values;
121
122 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530123 std::string expectedValue = "test0.50";
124 bool found = isValueFound(values, expectedValue);
125 EXPECT_EQ(found, true);
126
Ratan Guptac27170a2017-11-22 15:44:42 +0530127 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530128 expectedValue = "vlan";
129 found = isValueFound(values, expectedValue);
130 EXPECT_EQ(found, true);
131
Ratan Guptac27170a2017-11-22 15:44:42 +0530132 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530133 expectedValue = "50";
134 found = isValueFound(values, expectedValue);
135 EXPECT_EQ(found, true);
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530136}
137
138TEST_F(TestVlanInterface, deleteVLAN)
139{
140 createVlan(50);
141 deleteVlan("test0.50");
142 bool fileFound = false;
143
144 fs::path filePath = confDir;
145 filePath /= "test0.50.netdev";
146 if (fs::is_regular_file(filePath.string()))
147 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500148 fileFound = true;
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530149 }
150 EXPECT_EQ(fileFound, false);
151}
152
153TEST_F(TestVlanInterface, createMultipleVLAN)
154{
155 createVlan(50);
156 createVlan(60);
157
158 fs::path filePath = confDir;
159 filePath /= "test0.50.netdev";
160 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530161 config::ReturnCode rc = config::ReturnCode::SUCCESS;
162 config::ValueList values;
163
164 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530165 std::string expectedValue = "test0.50";
166 bool found = isValueFound(values, expectedValue);
167 EXPECT_EQ(found, true);
168
Ratan Guptac27170a2017-11-22 15:44:42 +0530169 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530170 expectedValue = "vlan";
171 found = isValueFound(values, expectedValue);
172 EXPECT_EQ(found, true);
173
Ratan Guptac27170a2017-11-22 15:44:42 +0530174 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530175 expectedValue = "50";
176 found = isValueFound(values, expectedValue);
177 EXPECT_EQ(found, true);
178
179 filePath = confDir;
180 filePath /= "test0.60.netdev";
181 parser.setFile(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530182 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530183 expectedValue = "test0.60";
184 found = isValueFound(values, expectedValue);
185 EXPECT_EQ(found, true);
186
Ratan Guptac27170a2017-11-22 15:44:42 +0530187 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530188 expectedValue = "60";
189 found = isValueFound(values, expectedValue);
190 EXPECT_EQ(found, true);
191
192 deleteVlan("test0.50");
193 deleteVlan("test0.60");
194}
195
Gunnar Mills57d9c502018-09-14 14:42:34 -0500196} // namespace network
197} // namespace phosphor