Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 1 | #include "config_parser.hpp" |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 2 | #include "ipaddress.hpp" |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 3 | #include "mock_network_manager.hpp" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 4 | #include "mock_syscall.hpp" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 5 | #include "vlan_interface.hpp" |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 6 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 8 | #include <net/if.h> |
| 9 | #include <netinet/in.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 10 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 11 | #include <filesystem> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 12 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 13 | #include <stdplus/gtest/tmp.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 14 | |
| 15 | #include <gtest/gtest.h> |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace network |
| 20 | { |
| 21 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 22 | namespace fs = std::filesystem; |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 23 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 24 | class TestVlanInterface : public stdplus::gtest::TestWithTmp |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 25 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 26 | public: |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 27 | sdbusplus::bus_t bus; |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 28 | std::string confDir; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 29 | MockManager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 30 | EthernetInterface interface; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 31 | TestVlanInterface() : |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 32 | bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()), |
| 33 | manager(bus, "/xyz/openbmc_test/network", confDir), |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 34 | interface(makeInterface(bus, manager)) |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 35 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 36 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 37 | } |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 38 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 39 | static EthernetInterface makeInterface(sdbusplus::bus_t& bus, |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 40 | MockManager& manager) |
| 41 | { |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 42 | mock_clear(); |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 43 | mock_addIF("test0", 1); |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 44 | return {bus, |
| 45 | "/xyz/openbmc_test/network/test0", |
| 46 | EthernetInterface::DHCPConf::none, |
| 47 | manager, |
| 48 | false, |
| 49 | true}; |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 52 | void createVlan(VlanId id) |
| 53 | { |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 54 | std::string ifname = "test0."; |
| 55 | ifname += std::to_string(id); |
| 56 | mock_addIF(ifname.c_str(), 1000 + id); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 57 | 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 Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 74 | { |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 75 | return false; |
| 76 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 77 | 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 Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 94 | interface.ip(addressType, ipaddress, subnetMask, gateway); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 95 | } |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | TEST_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 Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 105 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 106 | 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 Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | TEST_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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 122 | fileFound = true; |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 123 | } |
| 124 | EXPECT_EQ(fileFound, false); |
| 125 | } |
| 126 | |
| 127 | TEST_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 III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 135 | 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 Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 139 | |
| 140 | filePath = confDir; |
| 141 | filePath /= "test0.60.netdev"; |
| 142 | parser.setFile(filePath.string()); |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 143 | 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 Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 147 | |
| 148 | deleteVlan("test0.50"); |
| 149 | deleteVlan("test0.60"); |
| 150 | } |
| 151 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 152 | } // namespace network |
| 153 | } // namespace phosphor |