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 | |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 11 | #include <exception> |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 12 | #include <filesystem> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 13 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 14 | #include <stdplus/gtest/tmp.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 15 | |
| 16 | #include <gtest/gtest.h> |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 17 | |
| 18 | namespace phosphor |
| 19 | { |
| 20 | namespace network |
| 21 | { |
| 22 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 23 | namespace fs = std::filesystem; |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 24 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 25 | class TestVlanInterface : public stdplus::gtest::TestWithTmp |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 26 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 27 | public: |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 28 | sdbusplus::bus_t bus; |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 29 | std::string confDir; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 30 | MockManager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 31 | EthernetInterface interface; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 32 | TestVlanInterface() : |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 33 | bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()), |
| 34 | manager(bus, "/xyz/openbmc_test/network", confDir), |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 35 | interface(makeInterface(bus, manager)) |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 36 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 37 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 38 | } |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 39 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 40 | static EthernetInterface makeInterface(sdbusplus::bus_t& bus, |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 41 | MockManager& manager) |
| 42 | { |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 43 | mock_clear(); |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 44 | mock_addIF("test0", 1); |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 45 | return {bus, |
| 46 | "/xyz/openbmc_test/network/test0", |
| 47 | EthernetInterface::DHCPConf::none, |
| 48 | manager, |
| 49 | false, |
| 50 | true}; |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 53 | void createVlan(VlanId id) |
| 54 | { |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 55 | std::string ifname = "test0."; |
| 56 | ifname += std::to_string(id); |
| 57 | mock_addIF(ifname.c_str(), 1000 + id); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 58 | 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 Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 75 | { |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 76 | return false; |
| 77 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 78 | 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 Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 95 | interface.ip(addressType, ipaddress, subnetMask, gateway); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | bool isValueFound(const std::vector<std::string>& values, |
| 99 | const std::string& expectedValue) |
| 100 | { |
| 101 | for (const auto& value : values) |
| 102 | { |
| 103 | if (expectedValue == value) |
| 104 | { |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | return false; |
| 109 | } |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | TEST_F(TestVlanInterface, createVLAN) |
| 113 | { |
| 114 | createVlan(50); |
| 115 | fs::path filePath = confDir; |
| 116 | filePath /= "test0.50.netdev"; |
| 117 | |
| 118 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 119 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 120 | config::ValueList values; |
| 121 | |
| 122 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 123 | std::string expectedValue = "test0.50"; |
| 124 | bool found = isValueFound(values, expectedValue); |
| 125 | EXPECT_EQ(found, true); |
| 126 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 127 | std::tie(rc, values) = parser.getValues("NetDev", "Kind"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 128 | expectedValue = "vlan"; |
| 129 | found = isValueFound(values, expectedValue); |
| 130 | EXPECT_EQ(found, true); |
| 131 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 132 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 133 | expectedValue = "50"; |
| 134 | found = isValueFound(values, expectedValue); |
| 135 | EXPECT_EQ(found, true); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | TEST_F(TestVlanInterface, deleteVLAN) |
| 139 | { |
| 140 | createVlan(50); |
| 141 | deleteVlan("test0.50"); |
| 142 | bool fileFound = false; |
| 143 | |
| 144 | fs::path filePath = confDir; |
| 145 | filePath /= "test0.50.netdev"; |
| 146 | if (fs::is_regular_file(filePath.string())) |
| 147 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 148 | fileFound = true; |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 149 | } |
| 150 | EXPECT_EQ(fileFound, false); |
| 151 | } |
| 152 | |
| 153 | TEST_F(TestVlanInterface, createMultipleVLAN) |
| 154 | { |
| 155 | createVlan(50); |
| 156 | createVlan(60); |
| 157 | |
| 158 | fs::path filePath = confDir; |
| 159 | filePath /= "test0.50.netdev"; |
| 160 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 161 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 162 | config::ValueList values; |
| 163 | |
| 164 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 165 | std::string expectedValue = "test0.50"; |
| 166 | bool found = isValueFound(values, expectedValue); |
| 167 | EXPECT_EQ(found, true); |
| 168 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 169 | std::tie(rc, values) = parser.getValues("NetDev", "Kind"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 170 | expectedValue = "vlan"; |
| 171 | found = isValueFound(values, expectedValue); |
| 172 | EXPECT_EQ(found, true); |
| 173 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 174 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 175 | expectedValue = "50"; |
| 176 | found = isValueFound(values, expectedValue); |
| 177 | EXPECT_EQ(found, true); |
| 178 | |
| 179 | filePath = confDir; |
| 180 | filePath /= "test0.60.netdev"; |
| 181 | parser.setFile(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 182 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 183 | expectedValue = "test0.60"; |
| 184 | found = isValueFound(values, expectedValue); |
| 185 | EXPECT_EQ(found, true); |
| 186 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 187 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 188 | expectedValue = "60"; |
| 189 | found = isValueFound(values, expectedValue); |
| 190 | EXPECT_EQ(found, true); |
| 191 | |
| 192 | deleteVlan("test0.50"); |
| 193 | deleteVlan("test0.60"); |
| 194 | } |
| 195 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 196 | } // namespace network |
| 197 | } // namespace phosphor |