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> |
| 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 | |
| 24 | class TestVlanInterface : public testing::Test |
| 25 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 26 | public: |
| 27 | sdbusplus::bus::bus bus; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 28 | MockManager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 29 | EthernetInterface interface; |
| 30 | std::string confDir; |
| 31 | TestVlanInterface() : |
| 32 | bus(sdbusplus::bus::new_default()), |
| 33 | manager(bus, "/xyz/openbmc_test/network", "/tmp"), |
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 | { |
| 37 | setConfDir(); |
| 38 | } |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 39 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 40 | ~TestVlanInterface() |
| 41 | { |
| 42 | if (confDir != "") |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 43 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 44 | fs::remove_all(confDir); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 45 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 46 | } |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 47 | |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 48 | static EthernetInterface makeInterface(sdbusplus::bus::bus& bus, |
| 49 | MockManager& manager) |
| 50 | { |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 51 | mock_clear(); |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 52 | mock_addIF("test0", 1); |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 53 | return {bus, |
| 54 | "/xyz/openbmc_test/network/test0", |
| 55 | EthernetInterface::DHCPConf::none, |
| 56 | manager, |
| 57 | false, |
| 58 | true}; |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 61 | void setConfDir() |
| 62 | { |
| 63 | char tmp[] = "/tmp/VlanInterface.XXXXXX"; |
| 64 | confDir = mkdtemp(tmp); |
| 65 | manager.setConfDir(confDir); |
| 66 | } |
| 67 | |
| 68 | void createVlan(VlanId id) |
| 69 | { |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 70 | std::string ifname = "test0."; |
| 71 | ifname += std::to_string(id); |
| 72 | mock_addIF(ifname.c_str(), 1000 + id); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 73 | interface.createVLAN(id); |
| 74 | } |
| 75 | |
| 76 | void deleteVlan(const std::string& interfaceName) |
| 77 | { |
| 78 | interface.deleteVLANObject(interfaceName); |
| 79 | } |
| 80 | |
| 81 | int countIPObjects() |
| 82 | { |
| 83 | return interface.getAddresses().size(); |
| 84 | } |
| 85 | |
| 86 | bool isIPObjectExist(const std::string& ipaddress) |
| 87 | { |
| 88 | auto address = interface.getAddresses().find(ipaddress); |
| 89 | if (address == interface.getAddresses().end()) |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 90 | { |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 91 | return false; |
| 92 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool deleteIPObject(const std::string& ipaddress) |
| 97 | { |
| 98 | auto address = interface.getAddresses().find(ipaddress); |
| 99 | if (address == interface.getAddresses().end()) |
| 100 | { |
| 101 | return false; |
| 102 | } |
| 103 | address->second->delete_(); |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | void createIPObject(IP::Protocol addressType, const std::string& ipaddress, |
| 108 | uint8_t subnetMask, const std::string& gateway) |
| 109 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 110 | interface.ip(addressType, ipaddress, subnetMask, gateway); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | bool isValueFound(const std::vector<std::string>& values, |
| 114 | const std::string& expectedValue) |
| 115 | { |
| 116 | for (const auto& value : values) |
| 117 | { |
| 118 | if (expectedValue == value) |
| 119 | { |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | return false; |
| 124 | } |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | TEST_F(TestVlanInterface, createVLAN) |
| 128 | { |
| 129 | createVlan(50); |
| 130 | fs::path filePath = confDir; |
| 131 | filePath /= "test0.50.netdev"; |
| 132 | |
| 133 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 134 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 135 | config::ValueList values; |
| 136 | |
| 137 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 138 | std::string expectedValue = "test0.50"; |
| 139 | bool found = isValueFound(values, expectedValue); |
| 140 | EXPECT_EQ(found, true); |
| 141 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 142 | std::tie(rc, values) = parser.getValues("NetDev", "Kind"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 143 | expectedValue = "vlan"; |
| 144 | found = isValueFound(values, expectedValue); |
| 145 | EXPECT_EQ(found, true); |
| 146 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 147 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 148 | expectedValue = "50"; |
| 149 | found = isValueFound(values, expectedValue); |
| 150 | EXPECT_EQ(found, true); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(TestVlanInterface, deleteVLAN) |
| 154 | { |
| 155 | createVlan(50); |
| 156 | deleteVlan("test0.50"); |
| 157 | bool fileFound = false; |
| 158 | |
| 159 | fs::path filePath = confDir; |
| 160 | filePath /= "test0.50.netdev"; |
| 161 | if (fs::is_regular_file(filePath.string())) |
| 162 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 163 | fileFound = true; |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 164 | } |
| 165 | EXPECT_EQ(fileFound, false); |
| 166 | } |
| 167 | |
| 168 | TEST_F(TestVlanInterface, createMultipleVLAN) |
| 169 | { |
| 170 | createVlan(50); |
| 171 | createVlan(60); |
| 172 | |
| 173 | fs::path filePath = confDir; |
| 174 | filePath /= "test0.50.netdev"; |
| 175 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 176 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 177 | config::ValueList values; |
| 178 | |
| 179 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 180 | std::string expectedValue = "test0.50"; |
| 181 | bool found = isValueFound(values, expectedValue); |
| 182 | EXPECT_EQ(found, true); |
| 183 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 184 | std::tie(rc, values) = parser.getValues("NetDev", "Kind"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 185 | expectedValue = "vlan"; |
| 186 | found = isValueFound(values, expectedValue); |
| 187 | EXPECT_EQ(found, true); |
| 188 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 189 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 190 | expectedValue = "50"; |
| 191 | found = isValueFound(values, expectedValue); |
| 192 | EXPECT_EQ(found, true); |
| 193 | |
| 194 | filePath = confDir; |
| 195 | filePath /= "test0.60.netdev"; |
| 196 | parser.setFile(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 197 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 198 | expectedValue = "test0.60"; |
| 199 | found = isValueFound(values, expectedValue); |
| 200 | EXPECT_EQ(found, true); |
| 201 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 202 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 203 | expectedValue = "60"; |
| 204 | found = isValueFound(values, expectedValue); |
| 205 | EXPECT_EQ(found, true); |
| 206 | |
| 207 | deleteVlan("test0.50"); |
| 208 | deleteVlan("test0.60"); |
| 209 | } |
| 210 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 211 | } // namespace network |
| 212 | } // namespace phosphor |