blob: f7e6f2b444018e0f249f421d8050dac5169da27c [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);
William A. Kennington III26275a32021-07-13 20:32:42 -070053 return {bus,
54 "/xyz/openbmc_test/network/test0",
55 EthernetInterface::DHCPConf::none,
56 manager,
57 false,
58 true};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070059 }
60
Gunnar Mills57d9c502018-09-14 14:42:34 -050061 void setConfDir()
62 {
63 char tmp[] = "/tmp/VlanInterface.XXXXXX";
64 confDir = mkdtemp(tmp);
65 manager.setConfDir(confDir);
66 }
67
68 void createVlan(VlanId id)
69 {
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070070 std::string ifname = "test0.";
71 ifname += std::to_string(id);
72 mock_addIF(ifname.c_str(), 1000 + id);
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 interface.createVLAN(id);
74 }
75
76 void deleteVlan(const std::string& interfaceName)
77 {
78 interface.deleteVLANObject(interfaceName);
79 }
80
81 int countIPObjects()
82 {
83 return interface.getAddresses().size();
84 }
85
86 bool isIPObjectExist(const std::string& ipaddress)
87 {
88 auto address = interface.getAddresses().find(ipaddress);
89 if (address == interface.getAddresses().end())
Ratan Gupta2aca45c2017-07-26 01:16:27 +053090 {
Ratan Gupta2aca45c2017-07-26 01:16:27 +053091 return false;
92 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050093 return true;
94 }
95
96 bool deleteIPObject(const std::string& ipaddress)
97 {
98 auto address = interface.getAddresses().find(ipaddress);
99 if (address == interface.getAddresses().end())
100 {
101 return false;
102 }
103 address->second->delete_();
104 return true;
105 }
106
107 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
108 uint8_t subnetMask, const std::string& gateway)
109 {
Patrick Williams6aef7692021-05-01 06:39:41 -0500110 interface.ip(addressType, ipaddress, subnetMask, gateway);
Gunnar Mills57d9c502018-09-14 14:42:34 -0500111 }
112
113 bool isValueFound(const std::vector<std::string>& values,
114 const std::string& expectedValue)
115 {
116 for (const auto& value : values)
117 {
118 if (expectedValue == value)
119 {
120 return true;
121 }
122 }
123 return false;
124 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530125};
126
127TEST_F(TestVlanInterface, createVLAN)
128{
129 createVlan(50);
130 fs::path filePath = confDir;
131 filePath /= "test0.50.netdev";
132
133 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530134 config::ReturnCode rc = config::ReturnCode::SUCCESS;
135 config::ValueList values;
136
137 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530138 std::string expectedValue = "test0.50";
139 bool found = isValueFound(values, expectedValue);
140 EXPECT_EQ(found, true);
141
Ratan Guptac27170a2017-11-22 15:44:42 +0530142 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530143 expectedValue = "vlan";
144 found = isValueFound(values, expectedValue);
145 EXPECT_EQ(found, true);
146
Ratan Guptac27170a2017-11-22 15:44:42 +0530147 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530148 expectedValue = "50";
149 found = isValueFound(values, expectedValue);
150 EXPECT_EQ(found, true);
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530151}
152
153TEST_F(TestVlanInterface, deleteVLAN)
154{
155 createVlan(50);
156 deleteVlan("test0.50");
157 bool fileFound = false;
158
159 fs::path filePath = confDir;
160 filePath /= "test0.50.netdev";
161 if (fs::is_regular_file(filePath.string()))
162 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500163 fileFound = true;
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530164 }
165 EXPECT_EQ(fileFound, false);
166}
167
168TEST_F(TestVlanInterface, createMultipleVLAN)
169{
170 createVlan(50);
171 createVlan(60);
172
173 fs::path filePath = confDir;
174 filePath /= "test0.50.netdev";
175 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530176 config::ReturnCode rc = config::ReturnCode::SUCCESS;
177 config::ValueList values;
178
179 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530180 std::string expectedValue = "test0.50";
181 bool found = isValueFound(values, expectedValue);
182 EXPECT_EQ(found, true);
183
Ratan Guptac27170a2017-11-22 15:44:42 +0530184 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530185 expectedValue = "vlan";
186 found = isValueFound(values, expectedValue);
187 EXPECT_EQ(found, true);
188
Ratan Guptac27170a2017-11-22 15:44:42 +0530189 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530190 expectedValue = "50";
191 found = isValueFound(values, expectedValue);
192 EXPECT_EQ(found, true);
193
194 filePath = confDir;
195 filePath /= "test0.60.netdev";
196 parser.setFile(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530197 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530198 expectedValue = "test0.60";
199 found = isValueFound(values, expectedValue);
200 EXPECT_EQ(found, true);
201
Ratan Guptac27170a2017-11-22 15:44:42 +0530202 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530203 expectedValue = "60";
204 found = isValueFound(values, expectedValue);
205 EXPECT_EQ(found, true);
206
207 deleteVlan("test0.50");
208 deleteVlan("test0.60");
209}
210
Gunnar Mills57d9c502018-09-14 14:42:34 -0500211} // namespace network
212} // namespace phosphor