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"), |
| 34 | interface(bus, "/xyz/openbmc_test/network/test0", false, 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 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 48 | void setConfDir() |
| 49 | { |
| 50 | char tmp[] = "/tmp/VlanInterface.XXXXXX"; |
| 51 | confDir = mkdtemp(tmp); |
| 52 | manager.setConfDir(confDir); |
| 53 | } |
| 54 | |
| 55 | void createVlan(VlanId id) |
| 56 | { |
| 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 | { |
| 94 | interface.iP(addressType, ipaddress, subnetMask, gateway); |
| 95 | } |
| 96 | |
| 97 | bool isValueFound(const std::vector<std::string>& values, |
| 98 | const std::string& expectedValue) |
| 99 | { |
| 100 | for (const auto& value : values) |
| 101 | { |
| 102 | if (expectedValue == value) |
| 103 | { |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | return false; |
| 108 | } |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | TEST_F(TestVlanInterface, createVLAN) |
| 112 | { |
| 113 | createVlan(50); |
| 114 | fs::path filePath = confDir; |
| 115 | filePath /= "test0.50.netdev"; |
| 116 | |
| 117 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 118 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 119 | config::ValueList values; |
| 120 | |
| 121 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 122 | std::string expectedValue = "test0.50"; |
| 123 | bool found = isValueFound(values, expectedValue); |
| 124 | EXPECT_EQ(found, true); |
| 125 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 126 | std::tie(rc, values) = parser.getValues("NetDev", "Kind"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 127 | expectedValue = "vlan"; |
| 128 | found = isValueFound(values, expectedValue); |
| 129 | EXPECT_EQ(found, true); |
| 130 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 131 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 132 | expectedValue = "50"; |
| 133 | found = isValueFound(values, expectedValue); |
| 134 | EXPECT_EQ(found, true); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | TEST_F(TestVlanInterface, deleteVLAN) |
| 138 | { |
| 139 | createVlan(50); |
| 140 | deleteVlan("test0.50"); |
| 141 | bool fileFound = false; |
| 142 | |
| 143 | fs::path filePath = confDir; |
| 144 | filePath /= "test0.50.netdev"; |
| 145 | if (fs::is_regular_file(filePath.string())) |
| 146 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 147 | fileFound = true; |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 148 | } |
| 149 | EXPECT_EQ(fileFound, false); |
| 150 | } |
| 151 | |
| 152 | TEST_F(TestVlanInterface, createMultipleVLAN) |
| 153 | { |
| 154 | createVlan(50); |
| 155 | createVlan(60); |
| 156 | |
| 157 | fs::path filePath = confDir; |
| 158 | filePath /= "test0.50.netdev"; |
| 159 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 160 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 161 | config::ValueList values; |
| 162 | |
| 163 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 164 | std::string expectedValue = "test0.50"; |
| 165 | bool found = isValueFound(values, expectedValue); |
| 166 | EXPECT_EQ(found, true); |
| 167 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 168 | std::tie(rc, values) = parser.getValues("NetDev", "Kind"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 169 | expectedValue = "vlan"; |
| 170 | found = isValueFound(values, expectedValue); |
| 171 | EXPECT_EQ(found, true); |
| 172 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 173 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 174 | expectedValue = "50"; |
| 175 | found = isValueFound(values, expectedValue); |
| 176 | EXPECT_EQ(found, true); |
| 177 | |
| 178 | filePath = confDir; |
| 179 | filePath /= "test0.60.netdev"; |
| 180 | parser.setFile(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 181 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 182 | expectedValue = "test0.60"; |
| 183 | found = isValueFound(values, expectedValue); |
| 184 | EXPECT_EQ(found, true); |
| 185 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 186 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 187 | expectedValue = "60"; |
| 188 | found = isValueFound(values, expectedValue); |
| 189 | EXPECT_EQ(found, true); |
| 190 | |
| 191 | deleteVlan("test0.50"); |
| 192 | deleteVlan("test0.60"); |
| 193 | } |
| 194 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 195 | } // namespace network |
| 196 | } // namespace phosphor |