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 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 11 | #include <filesystem> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 12 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 13 | #include <stdplus/gtest/tmp.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 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 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 24 | class TestVlanInterface : public stdplus::gtest::TestWithTmp |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 25 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 26 | public: |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 27 | sdbusplus::bus_t bus; |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 28 | std::string confDir; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 29 | MockManager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 30 | EthernetInterface interface; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 31 | TestVlanInterface() : |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 32 | bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()), |
| 33 | manager(bus, "/xyz/openbmc_test/network", confDir), |
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 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 37 | } |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 38 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 39 | static EthernetInterface makeInterface(sdbusplus::bus_t& bus, |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 40 | MockManager& manager) |
| 41 | { |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 42 | mock_clear(); |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 43 | mock_addIF("test0", 1); |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 44 | return {bus, |
| 45 | "/xyz/openbmc_test/network/test0", |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 46 | config::Parser(), |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 47 | manager, |
| 48 | false, |
| 49 | true}; |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 52 | void createVlan(VlanId id) |
| 53 | { |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 54 | std::string ifname = "test0."; |
| 55 | ifname += std::to_string(id); |
| 56 | mock_addIF(ifname.c_str(), 1000 + id); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 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 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 94 | interface.ip(addressType, ipaddress, subnetMask, gateway); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 95 | } |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | TEST_F(TestVlanInterface, createVLAN) |
| 99 | { |
| 100 | createVlan(50); |
| 101 | fs::path filePath = confDir; |
| 102 | filePath /= "test0.50.netdev"; |
| 103 | |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 104 | config::Parser parser(filePath); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 105 | EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{ |
| 106 | {"NetDev", |
| 107 | { |
| 108 | {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}}, |
| 109 | }}, |
| 110 | {"VLAN", {{{"Id", {"50"}}}}}, |
| 111 | })); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | TEST_F(TestVlanInterface, deleteVLAN) |
| 115 | { |
| 116 | createVlan(50); |
| 117 | deleteVlan("test0.50"); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 118 | |
| 119 | fs::path filePath = confDir; |
| 120 | filePath /= "test0.50.netdev"; |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 121 | EXPECT_FALSE(fs::is_regular_file(filePath)); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST_F(TestVlanInterface, createMultipleVLAN) |
| 125 | { |
| 126 | createVlan(50); |
| 127 | createVlan(60); |
| 128 | |
| 129 | fs::path filePath = confDir; |
| 130 | filePath /= "test0.50.netdev"; |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 131 | config::Parser parser(filePath); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 132 | EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{ |
| 133 | {"NetDev", |
| 134 | { |
| 135 | {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}}, |
| 136 | }}, |
| 137 | {"VLAN", {{{"Id", {"50"}}}}}, |
| 138 | })); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 139 | |
| 140 | filePath = confDir; |
| 141 | filePath /= "test0.60.netdev"; |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 142 | parser.setFile(filePath); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 143 | EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{ |
| 144 | {"NetDev", |
| 145 | { |
| 146 | {{"Name", {"test0.60"}}, {"Kind", {"vlan"}}}, |
| 147 | }}, |
| 148 | {"VLAN", {{{"Id", {"60"}}}}}, |
| 149 | })); |
Ratan Gupta | 2aca45c | 2017-07-26 01:16:27 +0530 | [diff] [blame] | 150 | |
| 151 | deleteVlan("test0.50"); |
| 152 | deleteVlan("test0.60"); |
| 153 | } |
| 154 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 155 | } // namespace network |
| 156 | } // namespace phosphor |