blob: 85f4f40c8d9f298b7547ffa62c065b8b2bcfc925 [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);
Johnathan Mantey817012a2020-01-30 15:07:39 -080053 return {bus, "/xyz/openbmc_test/network/test0",
54 EthernetInterface::DHCPConf::none, manager};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070055 }
56
Gunnar Mills57d9c502018-09-14 14:42:34 -050057 void setConfDir()
58 {
59 char tmp[] = "/tmp/VlanInterface.XXXXXX";
60 confDir = mkdtemp(tmp);
61 manager.setConfDir(confDir);
62 }
63
64 void createVlan(VlanId id)
65 {
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070066 std::string ifname = "test0.";
67 ifname += std::to_string(id);
68 mock_addIF(ifname.c_str(), 1000 + id);
Gunnar Mills57d9c502018-09-14 14:42:34 -050069 interface.createVLAN(id);
70 }
71
72 void deleteVlan(const std::string& interfaceName)
73 {
74 interface.deleteVLANObject(interfaceName);
75 }
76
77 int countIPObjects()
78 {
79 return interface.getAddresses().size();
80 }
81
82 bool isIPObjectExist(const std::string& ipaddress)
83 {
84 auto address = interface.getAddresses().find(ipaddress);
85 if (address == interface.getAddresses().end())
Ratan Gupta2aca45c2017-07-26 01:16:27 +053086 {
Ratan Gupta2aca45c2017-07-26 01:16:27 +053087 return false;
88 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050089 return true;
90 }
91
92 bool deleteIPObject(const std::string& ipaddress)
93 {
94 auto address = interface.getAddresses().find(ipaddress);
95 if (address == interface.getAddresses().end())
96 {
97 return false;
98 }
99 address->second->delete_();
100 return true;
101 }
102
103 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
104 uint8_t subnetMask, const std::string& gateway)
105 {
106 interface.iP(addressType, ipaddress, subnetMask, gateway);
107 }
108
109 bool isValueFound(const std::vector<std::string>& values,
110 const std::string& expectedValue)
111 {
112 for (const auto& value : values)
113 {
114 if (expectedValue == value)
115 {
116 return true;
117 }
118 }
119 return false;
120 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530121};
122
123TEST_F(TestVlanInterface, createVLAN)
124{
125 createVlan(50);
126 fs::path filePath = confDir;
127 filePath /= "test0.50.netdev";
128
129 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530130 config::ReturnCode rc = config::ReturnCode::SUCCESS;
131 config::ValueList values;
132
133 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530134 std::string expectedValue = "test0.50";
135 bool found = isValueFound(values, expectedValue);
136 EXPECT_EQ(found, true);
137
Ratan Guptac27170a2017-11-22 15:44:42 +0530138 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530139 expectedValue = "vlan";
140 found = isValueFound(values, expectedValue);
141 EXPECT_EQ(found, true);
142
Ratan Guptac27170a2017-11-22 15:44:42 +0530143 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530144 expectedValue = "50";
145 found = isValueFound(values, expectedValue);
146 EXPECT_EQ(found, true);
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530147}
148
149TEST_F(TestVlanInterface, deleteVLAN)
150{
151 createVlan(50);
152 deleteVlan("test0.50");
153 bool fileFound = false;
154
155 fs::path filePath = confDir;
156 filePath /= "test0.50.netdev";
157 if (fs::is_regular_file(filePath.string()))
158 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500159 fileFound = true;
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530160 }
161 EXPECT_EQ(fileFound, false);
162}
163
164TEST_F(TestVlanInterface, createMultipleVLAN)
165{
166 createVlan(50);
167 createVlan(60);
168
169 fs::path filePath = confDir;
170 filePath /= "test0.50.netdev";
171 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530172 config::ReturnCode rc = config::ReturnCode::SUCCESS;
173 config::ValueList values;
174
175 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530176 std::string expectedValue = "test0.50";
177 bool found = isValueFound(values, expectedValue);
178 EXPECT_EQ(found, true);
179
Ratan Guptac27170a2017-11-22 15:44:42 +0530180 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530181 expectedValue = "vlan";
182 found = isValueFound(values, expectedValue);
183 EXPECT_EQ(found, true);
184
Ratan Guptac27170a2017-11-22 15:44:42 +0530185 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530186 expectedValue = "50";
187 found = isValueFound(values, expectedValue);
188 EXPECT_EQ(found, true);
189
190 filePath = confDir;
191 filePath /= "test0.60.netdev";
192 parser.setFile(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530193 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530194 expectedValue = "test0.60";
195 found = isValueFound(values, expectedValue);
196 EXPECT_EQ(found, true);
197
Ratan Guptac27170a2017-11-22 15:44:42 +0530198 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530199 expectedValue = "60";
200 found = isValueFound(values, expectedValue);
201 EXPECT_EQ(found, true);
202
203 deleteVlan("test0.50");
204 deleteVlan("test0.60");
205}
206
Gunnar Mills57d9c502018-09-14 14:42:34 -0500207} // namespace network
208} // namespace phosphor