blob: 5ee1cf9009d5b783a8483d1e681cd2e227d104c6 [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
105 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530106
William A. Kennington III25511a12022-08-04 16:32:28 -0700107 EXPECT_EQ(parser.getValues("NetDev", "Name"),
108 (config::ValueList{"test0.50"}));
109 EXPECT_EQ(parser.getValues("NetDev", "Kind"), (config::ValueList{"vlan"}));
110 EXPECT_EQ(parser.getValues("VLAN", "Id"), (config::ValueList{"50"}));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530111}
112
113TEST_F(TestVlanInterface, deleteVLAN)
114{
115 createVlan(50);
116 deleteVlan("test0.50");
117 bool fileFound = false;
118
119 fs::path filePath = confDir;
120 filePath /= "test0.50.netdev";
121 if (fs::is_regular_file(filePath.string()))
122 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500123 fileFound = true;
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530124 }
125 EXPECT_EQ(fileFound, false);
126}
127
128TEST_F(TestVlanInterface, createMultipleVLAN)
129{
130 createVlan(50);
131 createVlan(60);
132
133 fs::path filePath = confDir;
134 filePath /= "test0.50.netdev";
135 config::Parser parser(filePath.string());
William A. Kennington III25511a12022-08-04 16:32:28 -0700136 EXPECT_EQ(parser.getValues("NetDev", "Name"),
137 (config::ValueList{"test0.50"}));
138 EXPECT_EQ(parser.getValues("NetDev", "Kind"), (config::ValueList{"vlan"}));
139 EXPECT_EQ(parser.getValues("VLAN", "Id"), (config::ValueList{"50"}));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530140
141 filePath = confDir;
142 filePath /= "test0.60.netdev";
143 parser.setFile(filePath.string());
William A. Kennington III25511a12022-08-04 16:32:28 -0700144 EXPECT_EQ(parser.getValues("NetDev", "Name"),
145 (config::ValueList{"test0.60"}));
146 EXPECT_EQ(parser.getValues("NetDev", "Kind"), (config::ValueList{"vlan"}));
147 EXPECT_EQ(parser.getValues("VLAN", "Id"), (config::ValueList{"60"}));
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530148
149 deleteVlan("test0.50");
150 deleteVlan("test0.60");
151}
152
Gunnar Mills57d9c502018-09-14 14:42:34 -0500153} // namespace network
154} // namespace phosphor