blob: c4734b152622c7e77702199f1358d56d0a54f2f6 [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 manager,
48 false,
49 true};
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070050 }
51
Gunnar Mills57d9c502018-09-14 14:42:34 -050052 void createVlan(VlanId id)
53 {
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070054 std::string ifname = "test0.";
55 ifname += std::to_string(id);
56 mock_addIF(ifname.c_str(), 1000 + id);
Gunnar Mills57d9c502018-09-14 14:42:34 -050057 interface.createVLAN(id);
58 }
59
60 void deleteVlan(const std::string& interfaceName)
61 {
62 interface.deleteVLANObject(interfaceName);
63 }
64
65 int countIPObjects()
66 {
67 return interface.getAddresses().size();
68 }
69
70 bool isIPObjectExist(const std::string& ipaddress)
71 {
72 auto address = interface.getAddresses().find(ipaddress);
73 if (address == interface.getAddresses().end())
Ratan Gupta2aca45c2017-07-26 01:16:27 +053074 {
Ratan Gupta2aca45c2017-07-26 01:16:27 +053075 return false;
76 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 return true;
78 }
79
80 bool deleteIPObject(const std::string& ipaddress)
81 {
82 auto address = interface.getAddresses().find(ipaddress);
83 if (address == interface.getAddresses().end())
84 {
85 return false;
86 }
87 address->second->delete_();
88 return true;
89 }
90
91 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
92 uint8_t subnetMask, const std::string& gateway)
93 {
Patrick Williams6aef7692021-05-01 06:39:41 -050094 interface.ip(addressType, ipaddress, subnetMask, gateway);
Gunnar Mills57d9c502018-09-14 14:42:34 -050095 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +053096};
97
98TEST_F(TestVlanInterface, createVLAN)
99{
100 createVlan(50);
101 fs::path filePath = confDir;
102 filePath /= "test0.50.netdev";
103
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700104 config::Parser parser(filePath);
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700105 EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
106 {"NetDev",
107 {
108 {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
109 }},
110 {"VLAN", {{{"Id", {"50"}}}}},
111 }));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530112}
113
114TEST_F(TestVlanInterface, deleteVLAN)
115{
116 createVlan(50);
117 deleteVlan("test0.50");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530118
119 fs::path filePath = confDir;
120 filePath /= "test0.50.netdev";
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700121 EXPECT_FALSE(fs::is_regular_file(filePath));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530122}
123
124TEST_F(TestVlanInterface, createMultipleVLAN)
125{
126 createVlan(50);
127 createVlan(60);
128
129 fs::path filePath = confDir;
130 filePath /= "test0.50.netdev";
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700131 config::Parser parser(filePath);
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700132 EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
133 {"NetDev",
134 {
135 {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
136 }},
137 {"VLAN", {{{"Id", {"50"}}}}},
138 }));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530139
140 filePath = confDir;
141 filePath /= "test0.60.netdev";
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700142 parser.setFile(filePath);
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700143 EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
144 {"NetDev",
145 {
146 {{"Name", {"test0.60"}}, {"Kind", {"vlan"}}},
147 }},
148 {"VLAN", {{{"Id", {"60"}}}}},
149 }));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530150
151 deleteVlan("test0.50");
152 deleteVlan("test0.60");
153}
154
Gunnar Mills57d9c502018-09-14 14:42:34 -0500155} // namespace network
156} // namespace phosphor