blob: 8d31c7b2b975e51f4a19a8731be859d08035ec9a [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;
Ratan Gupta2aca45c2017-07-26 01:16:27 +053023
William A. Kennington III84bfe672022-07-13 14:15:30 -070024class TestVlanInterface : public stdplus::gtest::TestWithTmp
Ratan Gupta2aca45c2017-07-26 01:16:27 +053025{
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 public:
Patrick Williamsc38b0712022-07-22 19:26:54 -050027 sdbusplus::bus_t bus;
William A. Kennington III84bfe672022-07-13 14:15:30 -070028 std::string confDir;
Ratan Gupta35297172018-11-28 18:40:16 +053029 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050030 EthernetInterface interface;
Gunnar Mills57d9c502018-09-14 14:42:34 -050031 TestVlanInterface() :
William A. Kennington III84bfe672022-07-13 14:15:30 -070032 bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
33 manager(bus, "/xyz/openbmc_test/network", confDir),
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 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +053038
Patrick Williamsc38b0712022-07-22 19:26:54 -050039 static EthernetInterface makeInterface(sdbusplus::bus_t& bus,
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070040 MockManager& manager)
41 {
William A. Kennington III862275a2019-04-22 20:37:08 -070042 mock_clear();
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070043 mock_addIF("test0", 1);
William A. Kennington III26275a32021-07-13 20:32:42 -070044 return {bus,
45 "/xyz/openbmc_test/network/test0",
William A. Kennington IIIa520a392022-08-08 12:17:34 -070046 config::Parser(),
William A. Kennington III26275a32021-07-13 20:32:42 -070047 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 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +053097};
98
99TEST_F(TestVlanInterface, createVLAN)
100{
101 createVlan(50);
102 fs::path filePath = confDir;
103 filePath /= "test0.50.netdev";
104
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700105 config::Parser parser(filePath);
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700106 EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
107 {"NetDev",
108 {
109 {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
110 }},
111 {"VLAN", {{{"Id", {"50"}}}}},
112 }));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530113}
114
115TEST_F(TestVlanInterface, deleteVLAN)
116{
117 createVlan(50);
118 deleteVlan("test0.50");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530119
120 fs::path filePath = confDir;
121 filePath /= "test0.50.netdev";
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700122 EXPECT_FALSE(fs::is_regular_file(filePath));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530123}
124
125TEST_F(TestVlanInterface, createMultipleVLAN)
126{
127 createVlan(50);
128 createVlan(60);
129
130 fs::path filePath = confDir;
131 filePath /= "test0.50.netdev";
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700132 config::Parser parser(filePath);
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700133 EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
134 {"NetDev",
135 {
136 {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
137 }},
138 {"VLAN", {{{"Id", {"50"}}}}},
139 }));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530140
141 filePath = confDir;
142 filePath /= "test0.60.netdev";
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700143 parser.setFile(filePath);
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700144 EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
145 {"NetDev",
146 {
147 {{"Name", {"test0.60"}}, {"Kind", {"vlan"}}},
148 }},
149 {"VLAN", {{{"Id", {"60"}}}}},
150 }));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530151
152 deleteVlan("test0.50");
153 deleteVlan("test0.60");
154}
155
Gunnar Mills57d9c502018-09-14 14:42:34 -0500156} // namespace network
157} // namespace phosphor