William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 1 | #include "config_parser.hpp" |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 2 | #include "mock_network_manager.hpp" |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 3 | #include "mock_syscall.hpp" |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 4 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 5 | #include <arpa/inet.h> |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 6 | #include <net/if.h> |
| 7 | #include <netinet/in.h> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 10 | #include <filesystem> |
William A. Kennington III | f51620d | 2019-04-21 18:54:13 -0700 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 12 | #include <stdplus/gtest/tmp.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 13 | |
| 14 | #include <gtest/gtest.h> |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace network |
| 19 | { |
| 20 | |
William A. Kennington III | 9ecb90e | 2022-10-14 03:12:43 -0700 | [diff] [blame] | 21 | using ::testing::Key; |
| 22 | using ::testing::UnorderedElementsAre; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 23 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 24 | class TestNetworkManager : public stdplus::gtest::TestWithTmp |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 25 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 26 | protected: |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 27 | sdbusplus::bus_t bus; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 28 | MockManager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 29 | TestNetworkManager() : |
William A. Kennington III | f51620d | 2019-04-21 18:54:13 -0700 | [diff] [blame] | 30 | bus(sdbusplus::bus::new_default()), |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 31 | manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir()) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 32 | { |
William A. Kennington III | 7784856 | 2022-10-26 17:32:57 -0700 | [diff] [blame] | 33 | system::mock_clear(); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 34 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 35 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 36 | void createInterfaces() |
| 37 | { |
| 38 | manager.createInterfaces(); |
| 39 | } |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 40 | |
| 41 | void deleteVLAN(std::string_view ifname) |
| 42 | { |
| 43 | manager.interfaces.find(ifname)->second->vlan->delete_(); |
| 44 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | // getifaddrs will not return any interface |
| 48 | TEST_F(TestNetworkManager, NoInterface) |
| 49 | { |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 50 | createInterfaces(); |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 51 | EXPECT_TRUE(manager.interfaces.empty()); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 52 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 53 | // getifaddrs returns single interface. |
| 54 | TEST_F(TestNetworkManager, WithSingleInterface) |
| 55 | { |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 56 | // Adds the following ip in the getifaddrs list. |
William A. Kennington III | 7784856 | 2022-10-26 17:32:57 -0700 | [diff] [blame] | 57 | system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"}); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 58 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 59 | // Now create the interfaces which will call the mocked getifaddrs |
| 60 | // which returns the above interface detail. |
| 61 | createInterfaces(); |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 62 | EXPECT_THAT(manager.interfaces, UnorderedElementsAre(Key("igb1"))); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // getifaddrs returns two interfaces. |
| 66 | TEST_F(TestNetworkManager, WithMultipleInterfaces) |
| 67 | { |
William A. Kennington III | 7784856 | 2022-10-26 17:32:57 -0700 | [diff] [blame] | 68 | system::mock_addIF({.idx = 1, .flags = 0, .name = "igb0"}); |
| 69 | system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"}); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 70 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 71 | createInterfaces(); |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 72 | EXPECT_THAT(manager.interfaces, |
William A. Kennington III | 9ecb90e | 2022-10-14 03:12:43 -0700 | [diff] [blame] | 73 | UnorderedElementsAre(Key("igb0"), Key("igb1"))); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 74 | } |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 75 | |
| 76 | TEST_F(TestNetworkManager, WithVLAN) |
| 77 | { |
| 78 | EXPECT_THROW(manager.vlan("", 8000), std::exception); |
| 79 | EXPECT_THROW(manager.vlan("", 0), std::exception); |
| 80 | EXPECT_THROW(manager.vlan("eth0", 2), std::exception); |
| 81 | |
| 82 | system::mock_addIF({.idx = 1, .flags = 0, .name = "eth0"}); |
| 83 | manager.createInterfaces(); |
| 84 | EXPECT_NO_THROW(manager.vlan("eth0", 2)); |
| 85 | EXPECT_NO_THROW(manager.vlan("eth0", 4094)); |
| 86 | EXPECT_THAT( |
| 87 | manager.interfaces, |
| 88 | UnorderedElementsAre(Key("eth0"), Key("eth0.2"), Key("eth0.4094"))); |
| 89 | auto netdev1 = config::pathForIntfDev(CaseTmpDir(), "eth0.2"); |
| 90 | auto netdev2 = config::pathForIntfDev(CaseTmpDir(), "eth0.4094"); |
| 91 | EXPECT_TRUE(std::filesystem::is_regular_file(netdev1)); |
| 92 | EXPECT_TRUE(std::filesystem::is_regular_file(netdev2)); |
| 93 | |
| 94 | deleteVLAN("eth0.2"); |
| 95 | EXPECT_THAT(manager.interfaces, |
| 96 | UnorderedElementsAre(Key("eth0"), Key("eth0.4094"))); |
| 97 | EXPECT_FALSE(std::filesystem::is_regular_file(netdev1)); |
| 98 | EXPECT_TRUE(std::filesystem::is_regular_file(netdev2)); |
| 99 | } |
| 100 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 101 | } // namespace network |
| 102 | } // namespace phosphor |