blob: 499ab344fd5a2a00b5d928a0e6b1895b8ac87945 [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
Manojkiran Edaa879baa2020-06-13 14:39:08 +053011#include <filesystem>
Gunnar Mills57d9c502018-09-14 14:42:34 -050012#include <sdbusplus/bus.hpp>
William A. Kennington III84bfe672022-07-13 14:15:30 -070013#include <stdplus/gtest/tmp.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050014
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;
William A. Kennington IIId298f932022-10-17 14:31:38 -070023using std::literals::string_view_literals::operator""sv;
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 III9ecb90e2022-10-14 03:12:43 -070044 mock_addIF("test0", /*idx=*/1);
William A. Kennington III26275a32021-07-13 20:32:42 -070045 return {bus,
William A. Kennington III26275a32021-07-13 20:32:42 -070046 manager,
William A. Kennington IIId298f932022-10-17 14:31:38 -070047 getInterfaceInfo("test0"),
48 "/xyz/openbmc_test/network"sv,
49 config::Parser(),
50 /*emitSignal=*/false,
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070051 /*nicEnabled=*/true};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070052 }
53
William A. Kennington IIId298f932022-10-17 14:31:38 -070054 void createVlan(uint16_t id)
Gunnar Mills57d9c502018-09-14 14:42:34 -050055 {
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070056 std::string ifname = "test0.";
57 ifname += std::to_string(id);
58 mock_addIF(ifname.c_str(), 1000 + id);
Gunnar Mills57d9c502018-09-14 14:42:34 -050059 interface.createVLAN(id);
60 }
61
62 void deleteVlan(const std::string& interfaceName)
63 {
64 interface.deleteVLANObject(interfaceName);
65 }
66
67 int countIPObjects()
68 {
69 return interface.getAddresses().size();
70 }
71
72 bool isIPObjectExist(const std::string& ipaddress)
73 {
74 auto address = interface.getAddresses().find(ipaddress);
75 if (address == interface.getAddresses().end())
Ratan Gupta2aca45c2017-07-26 01:16:27 +053076 {
Ratan Gupta2aca45c2017-07-26 01:16:27 +053077 return false;
78 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050079 return true;
80 }
81
82 bool deleteIPObject(const std::string& ipaddress)
83 {
84 auto address = interface.getAddresses().find(ipaddress);
85 if (address == interface.getAddresses().end())
86 {
87 return false;
88 }
89 address->second->delete_();
90 return true;
91 }
92
93 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
94 uint8_t subnetMask, const std::string& gateway)
95 {
Patrick Williams6aef7692021-05-01 06:39:41 -050096 interface.ip(addressType, ipaddress, subnetMask, gateway);
Gunnar Mills57d9c502018-09-14 14:42:34 -050097 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +053098};
99
100TEST_F(TestVlanInterface, createVLAN)
101{
102 createVlan(50);
103 fs::path filePath = confDir;
104 filePath /= "test0.50.netdev";
105
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700106 config::Parser parser(filePath);
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700107 EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
108 {"NetDev",
109 {
110 {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
111 }},
112 {"VLAN", {{{"Id", {"50"}}}}},
113 }));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530114}
115
116TEST_F(TestVlanInterface, deleteVLAN)
117{
118 createVlan(50);
119 deleteVlan("test0.50");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530120
121 fs::path filePath = confDir;
122 filePath /= "test0.50.netdev";
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700123 EXPECT_FALSE(fs::is_regular_file(filePath));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530124}
125
126TEST_F(TestVlanInterface, createMultipleVLAN)
127{
128 createVlan(50);
129 createVlan(60);
130
131 fs::path filePath = confDir;
132 filePath /= "test0.50.netdev";
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700133 config::Parser parser(filePath);
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700134 EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
135 {"NetDev",
136 {
137 {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
138 }},
139 {"VLAN", {{{"Id", {"50"}}}}},
140 }));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530141
142 filePath = confDir;
143 filePath /= "test0.60.netdev";
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700144 parser.setFile(filePath);
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700145 EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
146 {"NetDev",
147 {
148 {{"Name", {"test0.60"}}, {"Kind", {"vlan"}}},
149 }},
150 {"VLAN", {{{"Id", {"60"}}}}},
151 }));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530152
153 deleteVlan("test0.50");
154 deleteVlan("test0.60");
155}
156
Gunnar Mills57d9c502018-09-14 14:42:34 -0500157} // namespace network
158} // namespace phosphor