Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 1 | #include "network_manager.hpp" |
| 2 | #include "mock_syscall.hpp" |
| 3 | #include "config_parser.hpp" |
| 4 | #include "vlan_interface.hpp" |
| 5 | #include "ipaddress.hpp" |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | #include <sdbusplus/bus.hpp> |
| 9 | |
| 10 | #include <net/if.h> |
| 11 | #include <netinet/in.h> |
| 12 | #include <arpa/inet.h> |
| 13 | #include <exception> |
| 14 | #include <experimental/filesystem> |
| 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace network |
| 19 | { |
| 20 | |
| 21 | namespace fs = std::experimental::filesystem; |
| 22 | |
| 23 | class TestVlanInterface : public testing::Test |
| 24 | { |
| 25 | public: |
| 26 | |
| 27 | sdbusplus::bus::bus bus; |
| 28 | Manager manager; |
| 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) |
| 35 | |
| 36 | { |
| 37 | setConfDir(); |
| 38 | } |
| 39 | |
| 40 | ~TestVlanInterface() |
| 41 | { |
| 42 | if(confDir != "") |
| 43 | { |
| 44 | fs::remove_all(confDir); |
| 45 | } |
| 46 | } |
| 47 | |
| 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()) |
| 74 | { |
| 75 | return false; |
| 76 | } |
| 77 | return true; |
| 78 | |
| 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, |
| 93 | const std::string& ipaddress, |
| 94 | uint8_t subnetMask, |
| 95 | const std::string& gateway) |
| 96 | { |
| 97 | interface.iP(addressType, |
| 98 | ipaddress, |
| 99 | subnetMask, |
| 100 | gateway |
| 101 | ); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | bool isValueFound(const std::vector<std::string>& values, |
| 106 | const std::string& expectedValue) |
| 107 | { |
| 108 | for (const auto& value : values) |
| 109 | { |
| 110 | if (expectedValue == value) |
| 111 | { |
| 112 | return true; |
| 113 | } |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | TEST_F(TestVlanInterface, createVLAN) |
| 120 | { |
| 121 | createVlan(50); |
| 122 | fs::path filePath = confDir; |
| 123 | filePath /= "test0.50.netdev"; |
| 124 | |
| 125 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame^] | 126 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 127 | config::ValueList values; |
| 128 | |
| 129 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 130 | std::string expectedValue = "test0.50"; |
| 131 | bool found = isValueFound(values, expectedValue); |
| 132 | EXPECT_EQ(found, true); |
| 133 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame^] | 134 | std::tie(rc, values) = parser.getValues("NetDev", "Kind"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 135 | expectedValue = "vlan"; |
| 136 | found = isValueFound(values, expectedValue); |
| 137 | EXPECT_EQ(found, true); |
| 138 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame^] | 139 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 140 | expectedValue = "50"; |
| 141 | found = isValueFound(values, expectedValue); |
| 142 | EXPECT_EQ(found, true); |
| 143 | |
| 144 | } |
| 145 | |
| 146 | TEST_F(TestVlanInterface, deleteVLAN) |
| 147 | { |
| 148 | createVlan(50); |
| 149 | deleteVlan("test0.50"); |
| 150 | bool fileFound = false; |
| 151 | |
| 152 | fs::path filePath = confDir; |
| 153 | filePath /= "test0.50.netdev"; |
| 154 | if (fs::is_regular_file(filePath.string())) |
| 155 | { |
| 156 | fileFound = true; |
| 157 | } |
| 158 | EXPECT_EQ(fileFound, false); |
| 159 | } |
| 160 | |
| 161 | TEST_F(TestVlanInterface, createMultipleVLAN) |
| 162 | { |
| 163 | createVlan(50); |
| 164 | createVlan(60); |
| 165 | |
| 166 | fs::path filePath = confDir; |
| 167 | filePath /= "test0.50.netdev"; |
| 168 | config::Parser parser(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame^] | 169 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 170 | config::ValueList values; |
| 171 | |
| 172 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 173 | std::string expectedValue = "test0.50"; |
| 174 | bool found = isValueFound(values, expectedValue); |
| 175 | EXPECT_EQ(found, true); |
| 176 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame^] | 177 | std::tie(rc, values) = parser.getValues("NetDev", "Kind"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 178 | expectedValue = "vlan"; |
| 179 | found = isValueFound(values, expectedValue); |
| 180 | EXPECT_EQ(found, true); |
| 181 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame^] | 182 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 183 | expectedValue = "50"; |
| 184 | found = isValueFound(values, expectedValue); |
| 185 | EXPECT_EQ(found, true); |
| 186 | |
| 187 | filePath = confDir; |
| 188 | filePath /= "test0.60.netdev"; |
| 189 | parser.setFile(filePath.string()); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame^] | 190 | std::tie(rc, values) = parser.getValues("NetDev", "Name"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 191 | expectedValue = "test0.60"; |
| 192 | found = isValueFound(values, expectedValue); |
| 193 | EXPECT_EQ(found, true); |
| 194 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame^] | 195 | std::tie(rc, values) = parser.getValues("VLAN", "Id"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 196 | expectedValue = "60"; |
| 197 | found = isValueFound(values, expectedValue); |
| 198 | EXPECT_EQ(found, true); |
| 199 | |
| 200 | deleteVlan("test0.50"); |
| 201 | deleteVlan("test0.60"); |
| 202 | } |
| 203 | |
| 204 | }// namespce network |
| 205 | }// namespace phosphor |