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