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