blob: 126d2d05fcff9d38bedb46e693e013f44afe76a6 [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",
46 EthernetInterface::DHCPConf::none,
47 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
104 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530105
William A. Kennington III25511a12022-08-04 16:32:28 -0700106 EXPECT_EQ(parser.getValues("NetDev", "Name"),
107 (config::ValueList{"test0.50"}));
108 EXPECT_EQ(parser.getValues("NetDev", "Kind"), (config::ValueList{"vlan"}));
109 EXPECT_EQ(parser.getValues("VLAN", "Id"), (config::ValueList{"50"}));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530110}
111
112TEST_F(TestVlanInterface, deleteVLAN)
113{
114 createVlan(50);
115 deleteVlan("test0.50");
116 bool fileFound = false;
117
118 fs::path filePath = confDir;
119 filePath /= "test0.50.netdev";
120 if (fs::is_regular_file(filePath.string()))
121 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500122 fileFound = true;
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530123 }
124 EXPECT_EQ(fileFound, false);
125}
126
127TEST_F(TestVlanInterface, createMultipleVLAN)
128{
129 createVlan(50);
130 createVlan(60);
131
132 fs::path filePath = confDir;
133 filePath /= "test0.50.netdev";
134 config::Parser parser(filePath.string());
William A. Kennington III25511a12022-08-04 16:32:28 -0700135 EXPECT_EQ(parser.getValues("NetDev", "Name"),
136 (config::ValueList{"test0.50"}));
137 EXPECT_EQ(parser.getValues("NetDev", "Kind"), (config::ValueList{"vlan"}));
138 EXPECT_EQ(parser.getValues("VLAN", "Id"), (config::ValueList{"50"}));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530139
140 filePath = confDir;
141 filePath /= "test0.60.netdev";
142 parser.setFile(filePath.string());
William A. Kennington III25511a12022-08-04 16:32:28 -0700143 EXPECT_EQ(parser.getValues("NetDev", "Name"),
144 (config::ValueList{"test0.60"}));
145 EXPECT_EQ(parser.getValues("NetDev", "Kind"), (config::ValueList{"vlan"}));
146 EXPECT_EQ(parser.getValues("VLAN", "Id"), (config::ValueList{"60"}));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530147
148 deleteVlan("test0.50");
149 deleteVlan("test0.60");
150}
151
Gunnar Mills57d9c502018-09-14 14:42:34 -0500152} // namespace network
153} // namespace phosphor